RESTAPIRequestBody - Scoped, Global

A RESTAPIRequestBody object allows you to access the body content of a scripted REST API request in scripts.

The format of a RESTAPIRequestBody object may be JSON or XML, depending on the content-type header value from the request.
Note: You cannot instantiate objects of this type. Objects of this type are created automatically and are accessible only in scripted REST API resource scripts.
Single entry example-request-body in JSON format.
{
 "name": "user1",
 "id": 1234,
 "roles": [
  {
   "name": "admin"
  },
  {
   "name": "itil"
  }
 ]
}
Multiple entry example-request-body in JSON format.
[{
 "name": "user1",
 "id": 1234,
 "roles": [
  {
   "name": "admin"
  },
  {
   "name": "itil"
  }
 ]
},{
 "name": "user2",
 "id":9876,
 "roles": [
  {
   "name":"admin"
  }
 ]
}]
Important: If the request body format is not Application/json, Application/xml, or Text/xml, use only the request body dataStream field to access the request body. Using request body data, dataString, nextEntry(), or hasNext() with a non-default format will result in a 500 error response.

RESTAPIRequestBody - data

The content of the request body.

Table 1. Field
Name Type Description
data Object or Array The request content. This can be a single object or an array of objects depending on the request.

var entry;
var id;
var requestBody = request.body;
var requestData = requestBody.data; //May be an array or a single object
if (requestData instanceof Array) { 
 entry = requestData[0].name; // 'user1'
 id = requestData[0].id; // '1234'
} else {
 entry = requestData.name; // 'user1'
 id = requestData.id; // '1234'
}

RESTAPIRequestBody - dataStream

The content of the request body, as a stream.

Table 2. Field
Name Type Description
dataStream Object The content of the request body. You can pass the stream to a separate API, such as to create an attachment from the request or forward the request to a different endpoint.
var requestBody = request.body; 
var requestString = requestBody.dataStream;

RESTAPIRequestBody - dataString

The content of the request body, as a String.

Table 3. Field
Name Type Description
dataString String The content of the request body.
var requestBody = request.body; 
var requestString = requestBody.dataString;

RESTAPIRequestBody - hasNext()

Determine if there are additional entries in the request body.

Use this method with the nextEntry() method to iterate over multiple request body entries.

Table 4. Parameters
Name Type Description
None
Table 5. Returns
Type Description
boolean True if there are additional entries available. This method returns true only once if the request contains a single entry.
var requestBody = request.body;
requestBody.hasNext(); // returns true if the request contains a single entry or multiple entries

//calling second time
requestBody.hasNext(); // returns false if the request contains a single entry, or true if the request contains multiple entries

RESTAPIRequestBody - nextEntry()

Retrieve one entry from the request body as a script object.

Use this method with the hasNext() method to iterate over multiple request body entries.

Table 6. Parameters
Name Type Description
None
Table 7. Returns
Type Description
Object A single entry from the request body.

var requestBody = request.body;
var requestEntry = requestBody.nextEntry(); // returns available entry if there is only one entry, or the first entry if there are multiple.
var name = requestEntry.name; // ‘user1’

// Calling second time
requestEntry = requestBody.nextEntry(); // returns undefined if there is only one entry, or the second entry if there are multiple.

This example demonstrates using hasNext() with nextEntry().

var requestBody = request.body;
while(requestBody.hasNext()){
var entry = requestBody.nextEntry();
}