Accessing Message Start Event (HTTP) with Python

In the previous article we started a Process by accessing the Message Start Event (HTTP) in Questetra BPM Suite using a curl command. In this chapter I will introduce a code example to do the same processing for Python 3. As we will use the Requests package for sending HTTP requests, please install it if you haven’t already.

We are going to use the same Workflow App as the previous curl Chapter. If you have not created it yet, please create and release the App referring to App Setting < Sending a variety of data in the curl Chapter and the Preparatory Chapter.

First, let’s try starting a Process in a state where only the Title has been entered. The code example is as follows.


import requests

if __name__ == '__main__':
    # API endpoint
    url = 'https://example.questetra.net/System/Event/MessageStart/{processModelInfoId}/{nodeNumber}/start'

    params = { # Store the parameters and the values for them in Dictionary
        'key' : {API key},
        'title' : 'TEST',
    }

    try:
        r = requests.post(url, data=params) # POST send
        r.raise_for_status()
        print('Status Code: {0}'.format(r.status_code))
        print(r.text)
    except requests.exceptions.HTTPError as e: # Catching HTTP error
        print('Error')
        print('Status Code: {0}'.format(r.status_code))
        print(r.text)
    except requests.exceptions.Timeout as e: # Cathing Timeout
        print('Timeout')
When sending character data to the Message Start Event (HTTP) API, you need to use UTF-8 for the character code. However, since Python 3 uses UTF-8 by default there is no need to configure the character codes.

By running this script you can start a Process with the title “TEST”. If you want to pass values to other parameters, add elements to params. Parameter names are key, data to be passed is the value. For example, except for the File-type parameter, the code becomes as follows:


params = {
    'key' : {API key},
    'title' : 'TEST',
    'q_str' : 'This is a test for Process Starting',
    'q_float' : 1.23,
    'q_selects' : 'false',
    'q_date' : '2018-04-01',
    'q_datetime' : '2018-04-01 12:34',
    'q_email' : 'questetra+Canarias@gmail.com',
    'q_group' : '10 Management department',
}

Like with curl, the procedure for sending data to File-type parameters is slightly different.


...
params = { # Parameters other than file type
    'key' : {API key},
    'title' : 'TEST',
}

import mimetypes # For finding MIME type

files = [ # File type parameter
        # (Parameter name, (file name, file object, MIME type))
bsp;      ('q_file', ('ques-kun-01.png', open('./ques-kun-01.png', 'rb'), mimetypes.guess_type('./ques-kun-01.png'))),
]

try:
    r = requests.post(url, data=params, files=files) # POST send

...

It is necessary to separate storage destinations for File-type and other type parameters. After doing so, pass it to the data and files parameters of the requests.post() method respectively. Unlike the curl command, you don’t have to worry about the data format when you send it as the Requests library automatically converts it to the application/x-www-form-urlencoded format for character string-only submissions, and to multipart/form-data format when files are included.

Finally, here is an example of the code for entering data into all Data Items and starting a Process.


import requests
import mimetypes

if __name__ == '__main__':
    # API endpoint
    url = 'https://example.questetra.net/System/Event/MessageStart/{processModelInfoId}/{nodeNumber}/start'


    params = { # Parameters other than file type
        'key' : {API key},
        'title' : 'TEST',
        'q_str' : 'This is a test for Process Starting',
        'q_float' : 1.23,
        'q_selects' : 'false',
        'q_date' : '2018-04-01',
        'q_datetime' : '2018-04-01 12:34',
        'q_email' : questetra+Canarias@gmail.com,
        'q_group' : '10 Management department',
    }

    files = [ # File type parameter
        # Multiple files also available
        ('q_file', ('ques-kun-01.png', open('./ques-kun-01.png', 'rb'), mimetypes.guess_type('./ques-kun-01.png'))),
        ('q_file', ('ques-kun-02.png', open('./ques-kun-02.png', 'rb'), mimetypes.guess_type('./ques-kun-02.png'))),
    ]

    try:
        r = requests.post(url, data=params, files=files) # POST send
        r.raise_for_status()
        print('Headers: ')
        print(r.request.headers)
        print('Status Code: {0}'.format(r.status_code))
        print(r.text)
    except requests.exceptions.HTTPError as e: # Catching HTTP error
        print('Error')
        print('Status Code: {0}'.format(r.status_code))
        print(r.text)
    except requests.exceptions.Timeout as e: # Cathing Timeout
        print('Timeout')

As you can see, using Python you can easily start a Process by preparing a set of parameters and values to pass over. If you utilize the API in the program you are using for your business you can determine the value to pass to the parameters and automatically start the Process depending on the situation. By all means, consider utilization of the Message Start Event (HTTP) as a way to automate your business!

Next: Starting a Process from Outside of Questetra BPM Suite (Error Chapter)

%d bloggers like this: