/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
setinsertsitefindsSETINSERTSITE. - 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#
| Variable | Holds |
|---|---|
request | the request; getQueryParam(name), getHeader(name) and getUserInfo() |
userInfo | the calling user; neither IBM page names it |
httpMethod | GET, POST, or PATCH for a POST with x-method-override: PATCH |
requestBody | the body as text, decoded as UTF-8; missing on GET and with an empty body |
responseHeaders | a map for the answer’s headers; see below |
service | the 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#
responseBodyis the answer, as text or as bytes. Text is sent as UTF-8. Any other type, such as a number, fails. Without aresponseBody, the answer is HTTP 200 with an empty body.- The content type is
application/jsonunless 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
responseHeadersgo back to the caller. In the 9.2 series, the handler reads onlycontent-type. On our test system, a script setcontent-typetotext/plainand added a headerx-bok-test. The answer came back astext/plain, andx-bok-testwas 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:
| Call | Answer |
|---|---|
?siteid=BEDFORD | {"siteid": "BEDFORD", "waiting": 495} |
?siteid=BEDFORD' or '1'='1 | "waiting": 0: the quote stays part of the value |
no siteid | HTTP 400 with the message for an invalid site |
Three habits make this shape safe:
- Bound values.
SqlFormatwithsetObjectputs 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.errorstops 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.
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#
| Topic | IBM’s pages | What Manage does |
|---|---|---|
responseHeaders | every header goes back to the caller | only content-type is used; other headers are dropped |
| The variables | five variables | also userInfo; no mbo and no scriptName |
| The insert-site example | changes the site for the session | the 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:
- Automation scripts in the IBM Maximo Manage documentation.
- All about Maximo scripting, IBM’s scripting guide on GitHub.
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.
