Skip to content

Instantly share code, notes, and snippets.

@chanakaudaya
Created March 30, 2023 13:11
Show Gist options
  • Save chanakaudaya/6583e069515e9d9ce615cb463806646f to your computer and use it in GitHub Desktop.
Save chanakaudaya/6583e069515e9d9ce615cb463806646f to your computer and use it in GitHub Desktop.
Mediation policy to call a backend and enrich payload in the request flow in WSO2 APIM
<sequence xmlns="http://ws.apache.org/ns/synapse" name="chainingSeq">
<!-- reading the author ID-->
<property name="uri.var.id" expression="json-eval($.author}" />
<!-- copying the original payload to a variable-->
<enrich>
<source type="body" clone="true"/>
<target type="property" property="original_request"/>
</enrich>
<!-- Saving the URL postfix that is required when calling the backend -->
<property name="rest_postfix" expression="$axis2:REST_URL_POSTFIX"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<!-- calling the backend service to get the name of the author from the ID-->
<call blocking="true">
<endpoint>
<http method="GET" uri-template="http://localhost:3000/profile/{uri.var.id}"/>
</endpoint>
</call>
<!-- Extract the name from the payload-->
<property name="author" expression="json-eval($.name}" />
<!-- Restore the original request payload to the message flow -->
<enrich>
<source clone="false" xpath="$ctx:original_request"/>
<target type="body"/>
</enrich>
<!-- Enrich the author name into the original payload -->
<enrich>
<source clone="true" property="author" type="property"/>
<target action="replace" xpath="json-eval($.author)"/>
</enrich>
<!-- Setting back the URL postfix to call the backend endpoint -->
<property name="REST_URL_POSTFIX" scope="axis2" action="set" expression="$ctx:rest_postfix"/>
</sequence>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment