WSPatch
In order to partially update an existing resource, you define the WSPatch attribute.
Syntax
WSPatch
Usage
You use this attribute to specify the action of the HTTP verb PATCH to partially update an existing resource. For instance, when you need to update the full resource, you use WSPut. When you just want to update a single field in a resource, you use WSPatch. The PATCH method is described in rfc5789 (external link).
You set the WSPatch
attribute in the ATTRIBUTES()
clause of the
function.
An output message body is not allowed in the response, so returns must be specified as headers with the WSHeader attribute.
Example using WSPatch to push partial resource change
In this sample REST function a user email address is updated. In the function's
id
parameter the attribute WSParam specifies the user to update,
and the newEmail
parameter contains the value to update. The
newEmail
data is passed in the message body in either JSON or XML format.
A string is returned as a header. It is specified with a WSHeader attribute.
WSThrows is set to handle errors. In the TRY/CATCH
block, the sqlca
record is checked after
the execution of the SQL query. The SQLERRMESSAGE
is set to the
message
field of the userError
variable, and a call to
SetRestError()
returns the message defined in WSThrows
for the error.
IMPORT com
PUBLIC DEFINE userError RECORD ATTRIBUTES(WSError = "User error")
message STRING
END RECORD
PUBLIC FUNCTION UpdateUserProfile(
id STRING ATTRIBUTE(WSParam), newEmail STRING)
ATTRIBUTES(WSPatch,
WSPath = "/users/{id}",
WSDescription = "Update user email address",
WSThrows = "400:@UserError,404:no user found")
RETURNS STRING ATTRIBUTE (WSHeader)
DEFINE ret STRING
TRY
UPDATE users SET email = newEmail
WHERE @id = id
IF sqlca.sqlerrd[3] = 1 THEN # sqlerrd[3] indicates processed rows
LET ret = SFMT("Updated user with ID: %1",id)
ELSE
CALL com.WebServiceEngine.SetRestError(404,NULL)
END IF
CATCH
LET ret=SFMT("Error updating user with ID: %1",id)
LET userError.message = SFMT("SQL error:%1 [%2]",
sqlca.sqlcode, SQLERRMESSAGE)
CALL com.WebServiceEngine.SetRestError(400,userError)
END TRY
RETURN ret
END FUNCTION