When calling a script via REST API, the framework provides a few implicit variables that you can utilize. For the inbound body you would need to use "requestBody" which will only be put in the hashmap if something exists (so if you sometimes provide something, and other times do not, be aware that trying to retrieve the variable might fail). If you need to return something to the calling program, you use the variable "responseBody" to build the response. In both cases, don't use the double quotes when referencing the variables.
If you need to retrieve query parameters or some other piece of data, there is a "request" implicit variable that exposes the OslcRequest. So for example, you can call:
request.getQueryParam("wonum")
And that would return the wonum query parameter if it was provided.
Both inbound and outbound, I would ensure you put this in a proper JSON format so you can easily parse it & build it. If you've never used the JSONObject java class, it's pretty simple. You would have something like this:
from com.ibm.json.java import JSONObject
requestBodyObject=JSONObject.parse(requestBody)
and then you would be able to do:
requestBodyObject.get("wonum")
------------------------------
Steven Shull
Director of Development
Projetech Inc
Cincinnati OH
------------------------------
Original Message:
Sent: Wed August 19, 2020 07:15 AM
From: Mahmoud Hossam
Subject: Getting information from request body in automation script
I am trying to make a POST request using REST API to run a script.
I want to send my request with a body, and use the body in my script.
eg. <hostname:port>/maxrest/oslc/script/myscript?_lid=wilson&_lpwd=wilson
body would be something like this
"wonum" : "1234"
I would like my automation script to get the wonum from the body of the request and put it in a variable to be used in the script later.
Is there a way I can get information from the POST request the invoked the script?
------------------------------
Mahmoud Hossam
------------------------------
#Maximo
#AssetandFacilitiesManagement