↓ Skip to main content
An ink and watercolour drawing of a small wooden box machine with a crank on a workbench and a brass call bell on top. A hand pushes a paper slip into a slot on the left side, and a folded slip comes out of a slot on the right side into a small wooden tray.

Your Own REST API With a Script

Maximo Automation Scripts - This article is part of a series.
Part 22: This Article
An automation script can be a REST endpoint of its own. Manage’s script handler runs it at /maximo/api/script/<NAME> and sends back what it sets. This article shows the call, the variables the handler really binds, the response rules, and an example that we ran on our test system.

This article opens Part 6, about REST API scripts. We checked IBM’s pages against the behaviour of Manage in the MAS 9.2 series, and we called test scripts over HTTP on our test system. Where the pages and Manage disagree, this page follows Manage.

The call
#

GET  /maximo/api/script/<NAME>
POST /maximo/api/script/<NAME>

Three rules apply to the name:

  • Upper case is applied for you. The handler upper-cases the name, so setinsertsite finds SETINSERTSITE.
  • Any script can be called. That includes a script with launch points. The handler runs it without a launch point, so the record of the launch point is missing.
  • An inactive script answers HTTP 200 with an empty body. The caller cannot tell it from a script that returns nothing.

A call without a name fails, and so does an unknown name. Article 23 covers who may call which script. By default, every authenticated user may call every one.

What the script gets
#

VariableHolds
requestthe request; getQueryParam(name), getHeader(name) and getUserInfo()
userInfothe calling user; neither IBM page names it
httpMethodGET, POST, or PATCH for a POST with x-method-override: PATCH
requestBodythe body as text, decoded as UTF-8; missing on GET and with an empty body
responseHeadersa map for the answer’s headers; see below
servicethe usual helper object

We read them on our test system. On a GET, requestBody did not exist. On a POST with the header x-method-override: PATCH, httpMethod was PATCH, and requestBody held the body.

Some familiar variables are missing. There is no mbo, so app, user, interactive and mboname are missing too. And there is no scriptName; service.getScriptName() returns the name.

Test for requestBody before you read it, because on a GET the name does not exist at all.

What the script returns
#

  • responseBody is the answer, as text or as bytes. Text is sent as UTF-8. Any other type, such as a number, fails. Without a responseBody, the answer is HTTP 200 with an empty body.
  • The content type is application/json unless the script sets another one: responseHeaders.put("content-type", "text/csv"). Write the key in lower case.
  • Every other header is dropped. IBM’s pages say that the headers in responseHeaders go back to the caller. In the 9.2 series, the handler reads only content-type. On our test system, a script set content-type to text/plain and added a header x-bok-test. The answer came back as text/plain, and x-bok-test was missing.

An error raised with service.error ends the call with an error answer. In our example below, that answer was HTTP 400 with Manage’s message.

An example that reads its input safely
#

This endpoint counts the waiting work orders of one site. The site comes from the query string, and it goes into the where clause as a bound value:

from psdi.mbo import SqlFormat
from com.ibm.json.java import JSONObject

site = request.getQueryParam("siteid")
if not site:
    service.error("system", "invalidsite", [str(site)])

sqf = SqlFormat("siteid = :1 and status = 'WAPPR'")
sqf.setObject(1, "WORKORDER", "SITEID", site)
wos = service.getMboSet("WORKORDER", userInfo)
try:
    wos.setWhere(sqf.format())
    result = JSONObject()
    result.put("siteid", site)
    result.put("waiting", wos.count())
    responseBody = result.serialize()
finally:
    wos.cleanup()

We ran it on our test system:

CallAnswer
?siteid=BEDFORD{"siteid": "BEDFORD", "waiting": 495}
?siteid=BEDFORD' or '1'='1"waiting": 0: the quote stays part of the value
no siteidHTTP 400 with the message for an invalid site

Three habits make this shape safe:

  • Bound values. SqlFormat with setObject puts the input into the where clause as a value. The second call shows the effect: a quote in the input changes nothing.
  • An early error. service.error stops the script on the line that found the problem.
  • Clean-up in finally. The script opened the set itself, so it closes it. Article 17 explains why.
One call through the script handler One call through the script handler
One call through the script handler

IBM’s example: the insert site
#

IBM’s example changes the user’s default insert site from a query parameter, and it comes in two versions:

  • The scripting guide’s copy sets the site on the user record and saves it, so the change is permanent. It also refreshes the cached login data of that user on this server. That second step is what reaches the running session.
  • The IBM Docs copy stops early. It never sets the site and never saves, so it changes nothing.

What IBM’s pages get wrong here
#

TopicIBM’s pagesWhat Manage does
responseHeadersevery header goes back to the calleronly content-type is used; other headers are dropped
The variablesfive variablesalso userInfo; no mbo and no scriptName
The insert-site examplechanges the site for the sessionthe guide’s copy saves it on the user record; the IBM Docs copy changes nothing

Article 33 of this series collects the corrections that matter most.

What comes next
#

Article 23 covers how to protect script endpoints.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 22: This Article