Set a request body
Functions that create or update a resource need to set a request body for the incoming payload. You specify the request body in an input parameter.
Typically, you specify an input body parameter when you perform an
HTTP PUT, POST, or PATCH request to a resource, otherwise you get error-9106. However, you can set
WSOptional
on the
ATTRIBUTE()
. For a sample function, see Set a request body as optional.
In your BDL function, you specify a request body by declaring an input parameter that does not
have any WSHeader
, WSQuery
, WSCookie
, or
WSParam
attributes.
The example
defines the single input parameter ("lname
") as a string. It 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
In the example, the "lname" input parameter is a STRING type. If the data type is not a primitive data type, you may need to define a type suitable for the structure in your module. The function needs to receive data of the required structure for the resource in either a JSON or XML representation. For an example, see Example: create resource with WSPost.
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