Skip to content

Extracting Data from HTTP Messages

Capturing HTTP Response in Context

APIO allows you to store the result of an HTTP call in the context dictionary. In the cell “HTTP call” we need to define the output context key: in the example below, this key is set to “rsp_releaseProfile_response”.

HTTP response

After the workflow has been instantiated, the dictionary context contains the new key rsp_releaseProfile_response with as value the HTTP response details (status and body) stored in a dictionary. We can use the dot notation in order to extract values as long as the dictionary key names are valid identifiers (when the key does not contain special characters or is a valid Python identifier)

Exercise

Copy/paste the text below in the Context panel of the Template Playground

json
    {
        "context":
        {
            "rsp_releaseProfile_response":
            {
                "body":
                {
                    "header":
                    {
                        "functionExecutionStatus":
                        {
                            "status": "Executed-Success"
                        }
                    }
                },
                "status": 200
            }
        }
    }

And use the dot notation in order to extract various values: for example for extracting the value of the attribute “functionExecutionStatus”, use

jinja
{{context.rsp_releaseProfile_response.body.header.functionExecutionStatus}}

On the other hand, if the key name does not conform with Python variable naming rules, we need to use the bracket notation, as shown in the following exercise.

Exercise

Copy/paste the text below in the Context panel of the Template Playground

json
    {
        "context":
        {
            "*response*":
            {
                "body":
                {
                    "header":
                    {
                        "functionExecutionStatus":
                        {
                            "status": "Executed-Success"
                        }
                    }
                },
                "status": 200
            }
        }
    }

And use the bracket notation in order to extract various values: for example for extracting the value of the attribute “functionExecutionStatus”, use

jinja
{{context['*response*'].body.header.functionExecutionStatus}}

Parsing XML Responses in APIO

When making XML calls in APIO, the response data is automatically parsed and stored in the output context key as a Python dictionary. This allows for easier handling and manipulation of the SOAP response within Jinja templates.

Example

For an XML SOAP response received in a 200 OK message, such as:

xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response resultCode="0">
    <description>Email changed successfully</description>
</response>

The value stored in the output context key will be:

json
{
    "body": {
        "response": {
            "-resultCode": "0",
            "description": "Email changed successfully"
        }
    },
    "status": 200
}

Key Points

  1. XML-to-Dictionary Conversion:

    • XML attributes (e.g., resultCode="0") are prefixed with a hyphen (-) when converted to dictionary keys (e.g., "-resultCode": "0").
    • XML elements (e.g., <description>) are converted into key-value pairs.
  2. Structure:

    • The body key contains the main content of the response.
    • The status key reflects the HTTP response status code (200 in this case).
  3. Ease of Use:

    • The parsed data is directly accessible in Jinja templates using dot notation. For example, if the output context key is set to vms_response, you can access the description with {{ context.vms_response.body.response.description }}.

Extracting Data from HTTP Request

The dictionary request contains the data contained in the HTTP request arriving at APIO and triggering the workflow. We can use the dot notation in order to extract various values (JSON body, method, …)

Exercise

Copy/paste the text below in the Context panel of the Template Playground.

json
    {
        "request":
        {
            "body":
            {
                "iccid": "8947010000123456784F",
                "profileType": "PLMN"
            },
            "content_type": "application/json",
            "match_info": null,
            "method": "POST",
            "params":
            {},
            "tail": "v1/esims/",
            "url": "/api/v01/custom/v1/esims/"
        }
    }

and extract the value of various keys.

Retrieving Information from the URL in APIO Workflows

Specifying the Method/Route

In the APIO GUI, under Orchestration > Startup Event, you can specify a method/route to define how a request triggers an "activity" (a workflow). A typical method/route might look like this:

GET /assets/{asset_id}/testdoc/{group_id}/services/{service_id}

Example Request

Given a request such as:

GET /assets/asset_87978/testdoc/grp_89797/services/srv_678686?isActive=true&data={"user":{"name":"John","age":30,"isVerified":true,"city":null}}

The APIO Request dictionary automatically extracts and organizes the information into the following structure:

json
{
    "request": {
        "match_info": {
            "asset_id": "asset_87978",
            "group_id": "grp_89797",
            "service_id": "srv_678686"
        },
        "method": "GET",
        "params": {
            "data": "{\"user\":{\"name\":\"John\",\"age\":30,\"isVerified\":true,\"city\":null}}",
            "isActive": "true"
        }
    }
}

Retrieving URL Path Information

Information from the URL path (e.g., asset_id, group_id, service_id) is stored in the request.match_info dictionary.

  • Example: To retrieve the asset_id, you can use:

    jinja
    {{ request.match_info.asset_id }}
    • Output: asset_87978
  • Converting to a JSON string: To return the value as a JSON string, you can use the | tojson filter:

    jinja
    {{ request.match_info.asset_id | tojson }}
    • Output: "asset_87978"

Retrieving URI Parameters

Query parameters from the URL are stored in the request.params dictionary as JSON-like strings. For instance:

  1. Accessing the isActive Parameter:

    jinja
    {{ request.params.isActive }}
    • Output: true

    As the isActive parameter contains a JSON-like string, you would need to use the | json filter to parse it into a Python boolean and get True as output.

  2. Accessing Nested Data: The data parameter contains a JSON string, so it needs to be parsed using the | json filter before accessing nested values:

    jinja
    {{ (request.params.data | json).user.age }}
    • Output: 30