RESTAPIRequestBody - Scoped, Global

  • Release version: Yokohama
  • Updated January 30, 2025
  • 3 minutes to read
  • The RESTAPIRequestBody API provides methods that allow 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.

    This API runs in the sn_ws namespace.

    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"
      }
     ]
    }]

    RESTAPIRequestBody - data

    The content of the request body.

    Note:
    REST web services consume data, datastream, and dataString via stream. Because streams are only consumed once, only one can be called. For example, if calling both data and dataString, the second call will return empty.
    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.

    Note:
    REST web services consume data, datastream, and dataString via stream. Because streams are only consumed once, only one can be called. For example, if calling both data and dataString, the second call will return empty.
    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 requestStream = requestBody.dataStream;

    RESTAPIRequestBody - dataString

    The content of the request body, as a String.

    Note:
    REST web services consume data, datastream, and dataString via stream. Because streams are only consumed once, only one can be called. For example, if calling both data and dataString, the second call will return empty.
    Table 3. Field
    Name Type Description
    dataString String The content of the request body.
    var requestBody = request.body; 
    var requestString = requestBody.dataString;

    RESTAPIRequestBody - hasNext()

    Determines 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 Flag indicating whether there are additional entries in the request body.
    Possible values:
    • true: Additional entries are available. As nextEntry() is called, hasNext() continues to be true until the last matching record is loaded.
    • false: No additional entries.
    var requestBody = request.body;
    requestBody.hasNext(); // returns true if the request contains a single entry or multiple entries
    requestBody. nextEntry(); // returns next available entry
    
    //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();
    }