Maximo

Maximo-ICON.png

Maximo

Learn how to increase the operational efficiency of the assets you manage, and improve overall equipment effectiveness by using IoT data and AI.

Maximo-ICON.png

TRIRIGA

Reduce the operational costs of the facilities you manage, and create more engaging occupant experiences through the application of IoT data and AI.

Maximo-ICON.png

Engineering

Learn how IoT data and AI are being applied to transform the end-to-end engineering lifecycle.

Expand all | Collapse all

Getting information from request body in automation script

Jump to Best Answer
  • 1.  Getting information from request body in automation script

    Posted Wed August 19, 2020 09:26 AM
    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
    ------------------------------


  • 2.  RE: Getting information from request body in automation script
    Best Answer

    Posted Thu August 20, 2020 08:37 AM
    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
    ------------------------------



  • 3.  RE: Getting information from request body in automation script

    Posted Mon August 24, 2020 06:51 AM
    Thank you very much Steven.
    I was able to retrieve values from the POST request body in my script thanks to the information you have provided.

    I would like to know what other implicit variables there is when interacting with REST API.
    Do you know any reference / documentation for the other variables?

    ------------------------------
    Mahmoud Hossam
    ------------------------------



  • 4.  RE: Getting information from request body in automation script

    Posted Mon August 24, 2020 07:35 AM

    The REST API documentation covers this a bit (https://developer.ibm.com/static/site-id/155/maximodev/restguide/Maximo_Nextgen_REST_API.html#_automation_scripts) but doesn't explicitly discuss the implicit variables (just references them). There was a PDF on automation script development that IBM is going to work on updating, but I don't have a recent link to it.

    For the variables, this may vary based on version, but at least in the latest (7.6.1.2), the following are variables that I see looking at the code (ScriptRouteHandler) in addition to the ones I talked about above:

    1) responseHeaders - Based on the name, I assume you can modify the headers returned on a response though haven't tested it myself
    2) userInfo - This avoids needing to call request.getUserInfo() if you're trying to open a set, get some information about the logged in user, etc.
    3) httpMethod - For seeing whether it was a POST/PUT/GET



    ------------------------------
    Steven Shull
    Director of Development
    Projetech Inc
    Cincinnati OH
    ------------------------------