Example: Update resource with WSPut

Update a resource with the WSPut attribute.

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.

Example updating a resource with WSPut

In this sample REST function a user resource is updated. In the function's id parameter, the attribute WSParam specifies the user to update and the thisUser variable of type profileType contains the values to update. The thisUser data is passed in the message body in either JSON or XML format.

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

TYPE profileType RECORD
     id INTEGER,
     name VARCHAR(100),
     email VARCHAR(255),
     category VARCHAR(10),
     ccode VARCHAR(3)
   END RECORD

PUBLIC DEFINE userError RECORD ATTRIBUTE(WSError = "User error")
  message STRING
END RECORD

PUBLIC FUNCTION updateUsers(
    id INTEGER ATTRIBUTES(WSParam),
    thisUser profileType)
  ATTRIBUTES(WSPut,
             WSPath = "/users/{id}",
             WSDescription = "Update a user profile",
             WSThrows = "400:@userError")
  RETURNS STRING
    DEFINE ret STRING
    TRY
      UPDATE users
        SET name = thisUser.name,
            email = thisUser.email,
            category = thisUser.category,
            ccode = thisUser.ccode
        WHERE id  = thisUser.id
       IF sqlca.sqlerrd[3] = 1 THEN # sqlerrd[3] indicates processed rows
         LET ret = SFMT("Updated user with ID: %1",id)
       ELSE
         LET ret = SFMT("No user with ID: %1",id)
       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