DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Prashant Sharma user avatar by
Prashant Sharma
·
Sep. 05, 18 · Tutorial
Like (1)
Save
Tweet
Share
7.79K Views

Join the DZone community and get the full member experience.

Join For Free

This 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:

  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, 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.

Extract

Published at DZone with permission of Prashant Sharma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using AI and Machine Learning To Create Software
  • Easy Smart Contract Debugging With Truffle’s Console.log
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • A Simple Union Between .NET Core and Python

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: