Accessing Message Start Event (HTTP) with Python

In the previous article, we started a Process by accessing Message Start Event (HTTP) of Questetra BPM Suite using a curl command. In this one, 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.

We are going to use the same Workflow App as the previous curl Chapter. If you have not created it, please create and release the App referring to “App Setting < Sending various data” in curl Chapter and 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, it does not need to set character codes.

By running this script, you can start a Process whose subject is “TEST”. If you want to pass values to other parameters, add elements to params. Parameter names are key, data to be passed are 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',
}

Likewise the case 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, respectively pass it to the data and files parameters of the requests.post() method. Also, please do file open in binary mode. Unlike the curl command, you do not have to care about the data format to send. By the Requests library, it is automatically converted to application/x-www-form-urlencoded format when character strings only, and to multipart/form-data format when files are included.

To the end, I will post the codes for entering data into all Data Items and Starting a Process, as an example.


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')

In this way, using Python, you can easily Start a Process by preparing a pair of parameters and values to pass. If you utilize the API in the program you are using for your business, you can automatically Start Processes determining the value to pass to the parameter depending on the situation. By all means, let’s 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)

2 thoughts on “Starting a Process from Outside of Questetra BPM Suite (Python Chapter)”

  1. Pingback: Starting a Process from Outside of Questetra BPM Suite (curl Chapter) – Questetra Support

  2. Pingback: Message Start Event (HTTP) – Questetra Support

Comments are closed.