Set a request body

Input parameters not specified with attributes are sent in the body of the HTTP PUT or POST request.

Note:

A message body is required when you perform an HTTP PUT or POST request to a resource, otherwise the request results in the error-9106.

If your function needs to create or update a resource, then you will need to set the request body for the incoming payload.

In your BDL function, you declare an input parameter without any of the REST parameter attributes (WSHeader, WSQuery, WSCookie). If you have more than one of such input parameters, it means your payload is delivered in a multipart form-data body request.

The function needs to receive data of the required structure for the resource in either a JSON or XML representation. If the data type is not a primitive data type, you may need to define a type suitable for the structure in your module. You reference the type in your input parameter. For an example, see Example: create resource with WSPost.

In this example the single input parameter ("lname") is not defined with a REST attribute and its content is therefore sent in the body of the HTTP request.

Example specifying a request body

IMPORT com

PUBLIC
DEFINE myerror RECORD ATTRIBUTE(WSError="My error")
  code INTEGER,
  reason STRING
END RECORD

PUBLIC FUNCTION updateUsersField(
    id INTEGER ATTRIBUTES(WSParam, WSDescription="User id") ),
    lname STRING ATTRIBUTE(WSDescription="User's last name"))
  ATTRIBUTES(WSPut,
             WSPath="/users/{id}",
             WSDescription="Update name of a given user",
             WSThrows="400:@myerror")
  RETURNS STRING
    DEFINE ret STRING
    WHENEVER ERROR CONTINUE
    UPDATE users SET 
         user_name=lname 
         WHERE @user_id = id
    WHENEVER ERROR STOP
    CASE
    WHEN SQLCA.SQLCODE == 0
         LET ret = SFMT("Updated user: %1",lname)
    WHEN SQLCA.SQLCODE == NOTFOUND
         LET ret = SFMT("No row was found with ID:  %1",id)
    OTHERWISE
        LET myerror.reason = SFMT("SQL error:%1 [%2]",SQLCA.SQLCODE, SQLERRMESSAGE)
        CALL com.WebServiceEngine.SetRestError(400,myerror)
    END CASE
    RETURN ret
END FUNCTION
Figure: Output of the HTTP request

Sample output of the HTTP request