↓ Skip to main content
A pen and ink drawing of a clerk's desk. On it lies an open teal file folder, and inside it two photographs of a machine part are fastened with an orange paper clip. A magnifying glass rests on the upper photograph and enlarges part of it. Beside the folder lie a small stack of photo prints and a teal dish of orange paper clips, with a rubber stamp, a pen pot, a plant and a fountain pen around the desk.

Attachments over the Maximo REST API

The Maximo REST API in Practice - This article is part of a series.
Part 9: This Article
A photo of a broken pump, a test report, a link to a manual: attachments travel with the record. The REST API handles them through one URI per record. This article shows how to upload a file, get it back, change its description and delete it, and which header mistakes cost an afternoon.

The rules here come from IBM’s documentation and from our own work with the REST API, on Manage in the MAS 9.2 series. We ran every request on our test system, against IBM’s demo data, as a user without administrator rights. The examples attach files to service requests through the object structure MXAPISR. The object structures for assets and work orders carry attachments the same way.

Where attachments live
#

Every record of these object structures has a doclinks entry with the URI of its attachments. It is there also when the record has none:

{
  "ticketid": "1229",
  "doclinks": {
    "href": "http://…/maximo/api/os/mxapisr/_U1IvMTIyOQ--/doclinks"
  }
}

An oslc.select that does not name doclinks leaves the entry out. From that URI, each attachment has two more:

One record, its attachment list, and two URIs per attachment One record, its attachment list, and two URIs per attachment
One record, its attachment list, and two URIs per attachment

The {id} is the number of the attachment in Manage. A GET on the list returns one member per attachment, each with href, localref and its metadata in describedBy. To get the list together with the record in one call, select it:

GET /maximo/api/os/mxapisr/<rest id>?lean=1&oslc.select=doclinks{*}

Upload a file
#

The body of the POST is the file itself, as raw bytes. Headers carry everything else:

POST /maximo/api/os/mxapisr/<rest id>/doclinks
slug: pump-check.txt
x-document-meta: FILE/Attachments
x-document-description: Inspection note
content-type: text/plain

Pump checked, no leak.

Manage answers 201 with an empty body. The Location header holds the URI of the file, ending in its id, and an ETag comes with it. The headers:

HeaderMeaning
slugthe file name; required
x-document-metathe URL type and the doc type, such as FILE/Attachments
x-document-descriptionthe description; without it, the file name
content-typethe type of the body
custom-encoding: base64the body is base64 text, Manage decodes it
content-locationthe address of a web link, see below

Attachments is the doc type, a folder of the attachment in Manage. The value alone, without FILE/, works too. Without any x-document-meta, the upload failed: the doc type is required, and Manage answered with BMXAA1515E, a message about error files that has nothing to do with the cause. An unknown doc type fails with BMXAA4191E.

What Manage does with the file name
#

We uploaded a few names on purpose. The slug becomes the file name in the store, after three changes:

  • White space goes. my report.txt is stored as myreport.txt.
  • A name that exists gets a number. A second pump-check.txt on the same record was stored with a long number before the extension. The first file stays.
  • The title is upper case. The metadata field title reads PUMP-CHECK.TXT, and fileName keeps the case.

The extension must be in the list of allowed file types. tool.exe fails with BMXAA6583E, and the message lists every allowed type of your system. Without slug, the request fails with 500 and a null pointer message.

The content-type trap
#

The type of the body decides little. Manage takes the type of the stored file from the extension of the name. One value breaks the upload: application/x-www-form-urlencoded. With it, Manage reads the POST as a query. It answered 200 with the description of the collection, and no file was attached.

That value is what curl -d sends by default. With curl, send the file with --data-binary and name a content type:

curl -X POST \
  "https://<host>/maximo/api/os/mxapisr/<rest id>/doclinks" \
  -H "apikey: $MAXIMO_APIKEY" \
  -H "slug: pump-check.txt" \
  -H "x-document-meta: FILE/Attachments" \
  -H "content-type: text/plain" \
  --data-binary @pump-check.txt

Download a file
#

A GET on the file URI returns the bytes. The response carries three useful headers:

  • Content-Type comes from the extension: text/plain for a .txt, image/png for a .png.
  • Link points to the metadata, in the form <…/doclinks/meta/77>;rel=describes.
  • Cache-Control allows the browser to keep the file for seven days.

Thumbnails
#

For an image, tn=1 returns a small version:

GET /maximo/api/os/mxapisr/<rest id>/doclinks/91?tn=1

We uploaded a picture of 400 by 300 pixels. With tn=1 it came back at 100 by 100, so the aspect ratio is lost. With tnw and tnh you set the size yourself: tn=1&tnw=200&tnh=150 returned 200 by 150. With tnw alone, Manage keeps the aspect ratio: tn=1&tnw=200 returned 200 by 150 too.

Part of a file
#

The Range header asks for some of the bytes. Manage answers 206 with a Content-Range header. One detail differs from the HTTP rule: Range: bytes=0-3 should return four bytes, and Manage returned three, with Content-Range: bytes 0-2/23. The last byte of the range is left out, so a client that downloads a large file in parts has to ask for one byte more.

The metadata
#

A GET on the meta URI returns JSON:

{
  "identifier": "77",
  "fileName": "pump-check.txt",
  "title": "PUMP-CHECK.TXT",
  "description": "Inspection note",
  "docType": "Attachments",
  "urlType": "FILE",
  "format": { "label": "text/plain" },
  "attachmentSize": 23,
  "created": "…",
  "createby": "…",
  "ownertable": "SR",
  "ownerid": 421
}

We shortened the answer. It holds more columns of the attachment, such as printthrulink and show, and the href of the meta URI. ownertable and ownerid name the record it belongs to.

A link to a web page#

An attachment can also be a link. The URL type is URL, and the address goes into content-location. The body stays empty:

POST /maximo/api/os/mxapisr/<rest id>/doclinks
slug: IBM docs
x-document-meta: URL/Attachments
content-location: https://www.ibm.com/docs/en/masv-and-l/cd

A GET on that attachment returns the address as text, with the same address in the Content-Location header. Its metadata has urlType URL and contentLocation, and no fileName. The doc type is required here too: with content-location and no x-document-meta, the upload failed with BMXAA4195E, “A value is required for the Document Folder field”.

FILE and URL are the values IBM ships. They belong to a synonym domain, so an installation can rename them. The domain article shows how to look up the values of a domain.

Change and delete
#

The file of an attachment cannot be replaced. To change it, delete the attachment and upload the new file. The metadata can change: a PATCH on the meta URI with a JSON body sets it.

POST /maximo/api/os/mxapisr/<rest id>/doclinks/meta/77?lean=1
x-method-override: PATCH
content-type: application/json
{ "dcterms:description": "Inspection note, revised" }

Manage answers 204. The keys need a prefix with a colon, here dcterms:. A plain description failed with 500 and a null pointer message. The fields that describe the file itself, such as title, do not change: the request answers 204 and the value stays.

To delete an attachment, send a DELETE to the file URI, or a POST with x-method-override: DELETE. Both answer 204, and the attachment is gone from the list. Deleting the record deletes its attachments too.

Attachments in the record JSON
#

The second way to attach a file is inside the JSON of the record. The file is base64 text in documentdata:

{
  "description": "Pump leaks at the seal",
  "siteid": "BEDFORD",
  "doclinks": [
    {
      "urltype": "FILE",
      "documentdata": "Q3JlYXRlZCB3aXRoIHRoZSByZWNvcmQu",
      "doctype": "Attachments",
      "urlname": "pump-photo.txt",
      "description": "Photo of the seal"
    }
  ]
}

This POST created the service request and its attachment in one call. A web link in the same array takes urltype URL and the address in urlname and newurlname. Give every entry a description: without it, Manage fills the description with the storage path of the file.

To read files back the same way, add inlinedoc=1 to a GET with oslc.select=doclinks{*}. Each attachment then holds its file in documentdata. The whole file goes into the JSON, so keep this for small files.

A PATCH with doclinks replaces all of them#

On an existing record, a doclinks array behaves like every other child array. The article on writing records explains it, and attachments are the place where it hurts most. We ran it on a record with two attachments:

  • A plain PATCH with one new entry in doclinks answered 204. Afterwards the record had one attachment, the new one. The two old ones were gone.
  • The same PATCH with patchtype: MERGE added the new entry, and the old attachments stayed.

So an update that adds an attachment through the record JSON needs patchtype: MERGE, or it uses the doclinks URI.

Errors at a glance
#

RequestAnswerFix
upload without slug500, null pointername the file in slug
upload without x-document-meta400 BMXAA1515Esend FILE/Attachments
an unknown doc type400 BMXAA4191Euse a doc type of your system
a web link without x-document-meta400 BMXAA4195Esend URL/Attachments
a file type that is not allowed400 BMXAA6583Ethe message lists the allowed types
content type x-www-form-urlencoded200, nothing attachedsend the file’s own type
PATCH on the metadata, no prefix500, null pointerwrite dcterms:description
PATCH with doclinks on a record204, old attachments goneadd patchtype: MERGE
Range: bytes=0-3206, three bytesask for one byte more

Examples
#

Each request below ran on our test system.

Upload a base64 file from a browser tool:

POST /maximo/api/os/mxapisr/<rest id>/doclinks
slug: b64.txt
x-document-meta: FILE/Attachments
custom-encoding: base64

RGVjb2RlZCBmcm9tIGJhc2U2NC4=

The stored file holds the decoded text, “Decoded from base64.”.

List the attachments with the record:

GET /maximo/api/os/mxapisr/<rest id>?lean=1
    &oslc.select=ticketid,doclinks{*}

Add an attachment through the record JSON and keep the others:

POST /maximo/api/os/mxapisr/<rest id>?lean=1
x-method-override: PATCH
patchtype: MERGE
{
  "doclinks": [
    {
      "urltype": "FILE",
      "documentdata": "Q3JlYXRlZCB3aXRoIHRoZSByZWNvcmQu",
      "doctype": "Attachments",
      "urlname": "merged.txt",
      "description": "Second photo"
    }
  ]
}

Get a 200 pixel preview of a photo:

GET /maximo/api/os/mxapisr/<rest id>/doclinks/<id>?tn=1&tnw=200

What comes next
#

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

The next article calls actions, workflows and automation scripts through the API. The previous article wrote many records in one request, and the series overview lists all parts. If you want help with the REST API in your own Manage, our services page shows how we work, or get in touch.

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

The Maximo REST API in Practice - This article is part of a series.
Part 9: This Article