SXI Forum

A place to collect usefull tips, tricks and implementation strategies.

You are not logged in.

#1 26-05-2021 14:46:07

SeanR
Administrator
Registered: 20-11-2018
Posts: 148

Python script to consume the XLayer Rest API

Below is an example script in python on how to consume the XLayerAPI v3.3+

import requests
from requests.structures import CaseInsensitiveDict

print("\n")
print("About to send REST Package")
print("==========================")
print()
url = "https://10.0.0.1:9788/xlayer-api/rest/async"

headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Basic c3hpX2NsaWVudDpQYXNzQDEyMw=="

data = """
{
    "integrationName": "FromPI-50",
    "actionName": "PI50-Python",
    "sourceId": "INC54321",
    "payload": {
        "test": "test string",
        "id": 12345
    }
}
"""

resp = requests.post(url, headers=headers, data=data, verify=False)

print(resp.status_code)
print(resp.text)

NOTES:

  1. Remember to replace the 10.0.0.1 ip with the XLayer servers IP

  2. verify=False - has been used as I was testing with a self-signed certificate

  3. The authorization header in this example is based on Basic Authentication, however you can replace this with the appropriate JWT header

  4. Be careful with the data field as this must be valid JSON.  IF your data string is not well formed you can expect an error along the lines of: {"timestamp":"2021-05-26T12:44:24.148+00:00","status":400,"error":"Bad Request","message":"","path":"/xlayer-api/rest/async"}

Offline

Board footer

Powered by FluxBB