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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python Techniques for Text Extraction From Images
  • OneStream Fast Data Extracts APIs
  • Reversing an Array: An Exploration of Array Manipulation
  • Setup ActiveMQ Artemis on Windows

Trending

  • The Retry Budget Pattern: How to Stop Retry Storms in API-Led and Microservice Systems
  • The Java Story: The Official Documentary Is Here
  • Calling GCP From AWS Without Static Keys Using Open-Source MultiCloudJ
  • How to Protect Your AI Agents from Prompt Injection Attacks: An Active Defense Approach

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.

By 
Prashant Sharma user avatar
Prashant Sharma
·
Updated Sep. 05, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
9.3K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Python Techniques for Text Extraction From Images
  • OneStream Fast Data Extracts APIs
  • Reversing an Array: An Exploration of Array Manipulation
  • Setup ActiveMQ Artemis on Windows

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook