Apigee: Extract The Header's Content
Let's take a look at extracting the header's content using Apigee. Let's also explore the Assign Message policy.
Join the DZone community and get the full member experience.
Join For FreeThis article was first published on the Knoldus blog.
Hello everyone, in my previous article, we saw how various Policies in Apigee make our life easy and our API faster and valuable. We saw Response Cache, Extract Variable, and Spike Arrest policy in action. If you want to go through the basics of Apigee or want to brush up your understanding of the Apigee policies, you can visit:
Basics of APIGEE and Understanding the Policies
Recently, while working on one of my projects, I came across a scenario where I was required to extract a header through APIGEE and save that header value in some other header combined with a timestamp so that this new header becomes a key for caching in Apigee. There was a collection of comma-separated names in the header and I was required to save them all in my new header along with the timestamp.
The most straightforward approach to this could be I supply an Assign Message Policy to my API proxy flow and in it, I extract out the content of my header add the timestamp to it and save it to a new header. Later in the Response Cache Policy, I set this new header as my key.
Let's see how this works out:
- Create assign message policy to extract out Header and add the timestamp value to it, set this new value into a new header.
- Create response cache policy to set this new header as a key.
The Assign Message policy would look like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="AccessRequest1">
<DisplayName>AccessRequest1</DisplayName>
<AssignTo createNew="false" type="request"/>
<Set>
<Headers>
<Header name="cacheKeyHeader">{request.header.user.values}{system.time}</Header>
</Headers>
</Set>
</AssignMessage>
and the Response Cache Policy would look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseCache async="false" continueOnError="false" enabled="true" name="ResponseCacheCacheHelloMessage">
<DisplayName>ResponseCache.CacheHelloMessage</DisplayName>
<CacheKey>
<KeyFragment ref="request.uri" type="string"/>
<KeyFragment ref="request.header.cacheKeyHeader.values" type="string"/>
</CacheKey>
<Scope>Application</Scope>
<ExpirySettings>
<TimeoutInSec ref="">60</TimeoutInSec>
</ExpirySettings>
</ResponseCache>
So I have a header named user that was passed in the incoming request, which contains a list of comma-separated usernames. In the Assign message policy I extracted user's content and appended a timestamp to it, then I assigned this new value to a new header named cacheKeyHeader.
To extract out the exact value passed to the header user, I used "{request.header.user.values}" the ".values" takes care of the multiple comma-separated usernames that we passed in the header, if I had not used the ".values" then only the first name before the first comma(,) would have been fetched from the header.
Later on in the Response cache policy, I set the cacheKeyHeader's value as my cache key with the following segment of code:
<KeyFragment ref="request.header.cacheKeyHeader.values" type="string"/>
So that is how we can Extract multiple comma-separated values out of a header in Apigee. For a sample hands-on with this scenario, one can clone in the APIProxy from here and then through Postman, you can try hitting my service endpoint.
Also, don't forget to pass on a header "user" to this endpoint through Postman. For example add Key = user and value = [user1,user2,user3] in the postman while hitting the above proxy endpoint.
The API proxy for the same can be found here.
Please feel free to comment or reach out to me in case of any queries.
This article was first published on the Knoldus blog.
Published at DZone with permission of Prashant Sharma, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments