Invoking an URL using Script

train rail during golden hour

Publish channels or Invocation channels are typically used to create Maximo exit integration points. However, it is possible to invoke an external API directly from its URL.

Getting data

To get data, you can use ScriptService class and its method httpgetasjson

service.httpgetasjson(<url>, <userid>, <headers>, <password>)

Sample:

from com.ibm.json.java import JSONObject, JSONArray

# Invoking an URL
url = "https://www.example.com/"
userid = ekishimoto
passwd = 12345678

# Invoke URL
response = service.httpgetasjson(url, userid, None, passwd)

status = response.getStatusLine().getStatusCode()
obj = JSONArray.parse(response.getEntity().getContent())

if status == 200:
	# OK
else:
	# error

Download Invoke URL – Getting Data source code

Posting data

To send data, you can use ScriptService class and its method httppostasjson

service.httppostasjson(<url>, <userid>, <password>, <headers>, <json>)

Sample:

from com.ibm.json.java import JSONObject, JSONArray

# create a JSON object
jsonObject = JSONObject()
jsonObject.put("equipment", mbo.getString("ASSETNUM"))
jsonObject.put("description", mbo.getString("DESCRIPTION"))

# Invoking an URL
url = "https://www.example.com/"
userid = None
passwd = None

# Invoke URL
response = service.httppostasjson(url, userid, passwd, None, jsonObject)

status = response.getStatusLine().getStatusCode()
obj = JSONArray.parse(response.getEntity().getContent())

if status == 200:
	# OK
else:
	# error

Download Invoke URL – Posting Data source code


If you found my post interesting or useful and just want to say thanks, you can always buy me a coffee.

Leave a Reply