Extract comma separated header value

How can I can extract complete value of a comma separated header ?.

filterValues =1,2,3

When I use extract variable/ JS it extracts 1st token & not the complete value..

var hdr = context.getVariable('request.header.filterValues'); context.setVariable("result", hdr);

-Vinay

Solved Solved
1 20 3,208
1 ACCEPTED SOLUTION

akoo
Participant V

Hi @vinay poreddy,

Please try the following:

var hdr = context.getVariable('request.header.filterValues.values')+'';

More details in another community post here.

View solution in original post

20 REPLIES 20

akoo
Participant V

Hi @vinay poreddy,

Please try the following:

var hdr = context.getVariable('request.header.filterValues.values')+'';

More details in another community post here.

Please test at your end let me know if it works? Not sure if it is as per HTTP spec.

==

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

==

Header Name ->filterValues

Header Values-> 1,2,3

comma separated values.

-Vinay

Please test at your end let me know if it works? Not sure if it is as per HTTP spec.

==

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

==

Header Name ->filterValues

Header Values-> 1,2,3

comma separated values.

-Vinay

Sample header..

it does

Huh..spending too much time 😞

After using below script I get the value but how to use the value as the output of var a results in 1,2,3 but when I assign it gives below

org.mozilla.javascript.NativeArray@51af7cb2

var hdr = context.getVariable('request.header.filterValues.values')+''; var a = hdr.substring(1, hdr.length-1).split(','); //context.setVariable("result",hdr); //print(hdr) print(a) context.setVariable("result1",a);

Attaching the proxy for reference.

-Vinaymultiheader-rev1-2016-04-071.zip

The sample code in your bundle converts the string to an array. But if you want an individual value, you need to set the array index and convert toString. There are more details in the post. Here is the revised JS code to get the first value in the array, index 0.

var hdr = context.getVariable('request.header.filterValues.values')+'';
var a = hdr.substring(1, hdr.length-1).split(',');
var b = a[0];
//context.setVariable("result",hdr);
//print(hdr)
context.setVariable("result1",b);
print(b)

If you found my help useful, please mark my answer as correct.

Thanks

I want to get complete header value - 1,2,3 value. I understand to extract individual values but how to extract complete value & use it? One of our application is sending custom headers as filterValues as 1,2,3 & in apigee we need to extract the header values (i.e 1,2,3)and later use it in a query param while requesting third party application.

You'll notice in the JS code that "hdr.substring(1, hdr.length-1)" is removing the square brackets, resulting in "1,2,3".

Already tested using below.How to set output of 'a' to a variable? It throws below..

org.mozilla.javascript.NativeArray@4e8a9d8

var hdr = context.getVariable('request.header.filterValues.values')+''; var a = hdr.substring(1, hdr.length-1).split(','); context.setVariable("result",a); print (a);

trace.jpeg

js.jpeg

Hi @vinay poreddy, let's take this off-line or into a new thread. I think the value of this post is diminishing by answering questions in the comments thread.

How can I assign & use the extracted value?

I want to get complete header value - 1,2,3 value. I understand to extract individual values but how to extract complete value & use it? One of our application is sending custom headers as filterValues as 1,2,3 & in apigee we need to extract the header value(i.e 1,2,3) and later use it in a query param while requesting third party application.

Below is the script which resolved the issue.

var hdr = context.getVariable('request.header.filterValues.values')+''; //context.setVariable("result1",hdr.toString()); var a = hdr.substring(1, hdr.length-1).split(','); context.setVariable("result",a.toString());

-Vinay

Not applicable

@vinay poreddy @Alex Koo

There really should be a way to get the raw header value. Although the HTTP spec describes the comma separated list syntax, the way Apigee is restricting the message.header.foobar implementation to get only the first value in a comma separated list seems like a far too literal interpretation of the HTTP spec. At the very least, there should be a method to get the raw header value as an alternative.

Hi @Chris Covney, the answer I posted below will give you the raw header value. A little further in the comments are details on how to extract the values of the array. Please let me know if that helps.

Hi @Alex Koo Thanks for the quick reply! I see that the provided answers are a workaround, and I have my app working using said workaround, however, it seems silly (and overly literal) that the default behavior of the message.header.foo is to get only the value up to the first comma. It would make more sense if the default behavior were to return the entire raw header string value. If getting the header value up to the first comma were so important, perhaps have an extra method to grab only the value up to the first comma.

Thanks for your feedback, @Chris Covney. I agree with your sentiments. We'll discuss with our product team to see how we can make CSV headers easier to work with.

A note for future readers: a while ago, not sure when, the pattern of using request.header.header_name.values.string  has been implemented as a way to get "all of the values" for a multi-valued header. Documentation here. and here.