Apigee — Extract The Header’s Content

Knoldus Inc.
Knoldus - Technical Insights
3 min readApr 4, 2018

Hello everyone, in my previous blog 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:

Basic Of APIGEE and Understanding the Policies

Recently while working on one of my projects I came across a scenario wherein 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:

  1. Create assign message policy to extract out Header and add the timestamp value to it, set this new value into a new header.
  2. 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, it 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 could be found here :

Please feel free to comment or reach out to me in case of any queries. :-)

knoldus-advt-sticker

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com