Reputation: 71
I created a REST API with the Integrator tooling. I'm able to make consecutively calls, but I don't know how to store and use the response from the first call as a header to call the next endpoint.
The scenario is: I have to first call to an endpoint to get one token, with the response I want to set a header with a bearer token to retrieve a JSON file calling the next endpoint.
My code:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/example" name="example" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log description="request log" level="full"/>
<payloadFactory description="body" media-type="json">
<format>
{
"grant_type": "authorization_code"
}
</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="post" statistics="enable" trace="enable" uri-template="http://localhost:5000/token">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
<property name="grant_type" scope="transport" value="authorization_code"/>
</endpoint>
</call>
<filter regex="200" source="get-property('axis2', 'HTTP_SC')">
<then>
<log level="custom">
<property name="switchlog" value="Case: first call successful"/>
</log>
<call>
<endpoint>
<http method="get" uri-template="http://localhost:5000/json">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<respond description="final"/>
</then>
<else>
<log level="custom">
<property name="switchlog" value="Case: first call unsuccessful"/>
</log>
<respond/>
</else>
</filter>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
How can I do it?
Upvotes: 0
Views: 509
Reputation: 84
You can store the token in the registry after the first call. It will allow the rest of the request to invoke the back using the token. Please refer to the Gmail connector implementation. Here we are using the script mediator to store the token in the registry. ( Filter logic is needed to skip the invocation of the token endpoint if we have a valid token in the registry also we should renew the token if it's expired ) Hope This sample mediation will help,
Upvotes: 1