Using ExtractVariable policy to extract HTTP header and printing on console using JavaScript

Hi,

I'm very new to APIGEE and have been going through 4MV4D by Anil to try out some scenarios. My scenario is very simple.

I have created a "Reverse Proxy" and mapped it to httpbin.org/get as target. I'm trying to use ExtractVariable policy to extract the "Content-Encoding" HTTP header and use a JavaScript policy to print to the console.

Here's my code from "ExtractVariable" policy:

....

<Header name="Content-Encoding">  
    <Pattern ignoreCase="true">CE {response.header.Content-Encoding}</Pattern>  
</Header>
<Source clearPayload="false">response</Source>

....

This is what I'm using to print to the console using JavaScript

print(context.getVariable("CE"));

Both the "ExtractVariable" policy and JavaScript policy is applied to the response path of the Pre-Flow.

The proxy executes successfully and I get a 200 OK and a successful response back in the browser, however, I'm seeing null being printed on the console.

What am I missing here? Can anyone please suggest?

0 1 128
1 REPLY 1

@Ram GKrishnan

Firstly, httpbin.org/get doesn't return Content-Encoding Header

Secondly you are trying to match a pattern like "CE 123", and you are trying to assign it to flow variable response.header.Content-Encoding. The value of response.header.Content-Encoding will be 123. You haven't defined a variable called CE, but you are trying to print the value of variable CE.

Below is a code snippet for Content-type header, where I am trying to match a pattern application/xxx

<Header name="Content-Type">
<Pattern ignoreCase="true">application/{matchedValue}</Pattern>
</Header>
<Source clearPayload="false">response</Source>

matchedValue is the variable that will contain the value or you could use

<Pattern ignoreCase="true">{matchedValue}</Pattern> to read the complete value into the variable.

The javascript can print the same with the below line of code

print(context.getVariable("matchedValue"));

Attaching the proxy code if that helps you.

no-target-rev1-2019-02-28-1.zip

trace-1551314809427.txt