Using 'me' instead of an ID

This pattern allows API users to specify that they want their own records when the back end API expects the ID to be passed.

For the following example:

There's several slightly different ways we could approach this problem, but for this example we will be using xpath to change the url before it's passed to the backend.

Create the API as per usual:

When a request https://api/studata/1.0/students/me by the APIM the incoming URL will be broken down by the APIM into the following parts:

  • context: studata
  • version: 1.0
  • resource: students/me
  • REST_URL_POSTFIX: students/me

The REST_URL_POSTFIX holds everything after the context/version and is appended to the backend URL before calling.  It's this value that we'll be manipulating in this example.

Sequence to replace me with pid
<sequence xmlns="http://ws.apache.org/ns/synapse" name="studata_v7">
    <!-- Transfer the claims from the JWT into context properties -->
    <class name="edu.ucsd.its.soa.wso2.esb.ucsd_custom_mediators.ExtractJWTClaims"/>
    <!-- Grab the pid claim and move it into a more manageable variable name -->
    <property name="pid" expression="get-property('claim:http://wso2.org/claims/pid')"/>
    <!-- Replace token 'me' with their PID -->
    <property scope="axis2" name="REST_URL_POSTFIX" expression="concat(substring-before($axis2:REST_URL_POSTFIX, '/me'), '/', $ctx:pid)"/>
</sequence>

Upload the above file as an incoming mediation sequence and the API should be ready to use.