Hi Everybody!
I am using service callout policy to get the CSRF token and also getting some response headers.
From response headers, am trying to extract header set-cookie but have two set-cookie headers with same header name.
Example:
Response headers:
set-cookie: abc
set-cookie:123
I am using variable calloutresponse.header.set-cookie but it was extracting value abc but I want to extract the value 123. please let me know the solution.
Thanks in advance!
Solved! Go to Solution.
Hello,
I have faced this problem for several days, I will try to provide you with as many details as possible so as to help other readers as well.
First of all, I would like to point out that by making a no-route example proxy and adding a policy that adds two "set-cookie" headers in request, you will be able to see in the debug that Apigee merges the two headers.
This is the policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM_set-cookie">
<DisplayName>AM_set-cookie</DisplayName>
<Properties/>
<Add>
<Headers>
<Header name="set-cookie">firstcookie=ABCDE</Header>
</Headers>
</Add>
<Add>
<Headers>
<Header name="set-cookie">secondcookie=12345</Header>
</Headers>
</Add>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
This is the debug output:
Apigee provides basic functionality to iterate these values (doc). As you can read, this is a Read-Only collection. You can use request.header.header_name.values.count to have the collection length and request.header.header_name.N to access to the values.
This is a sample JavaScript script with which you can try out these features.
// This return collection (https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html)
var set_cookie_collection = context.getVariable("response.header.set-cookie.values");
// Returns an array containing all of the elements in this collection.
var set_cookie_array = set_cookie_collection.toArray();
// All the values of a particular header in the request, in the form of a single string.
var set_cookie_string = context.getVariable("response.header.set-cookie.values.string");
// Returns the number of elements in this collection.
var set_cookie_length = context.getVariable("response.header.set-cookie.values.count");
print("set-cookie collection: " + set_cookie_collection);
print("set-cookie array: " + set_cookie_array);
print("set-cookie string: " + set_cookie_string);
print("number of set-cookie headers: " + set_cookie_length);
var cookie_to_find = "secondcookie";
// When you use request.header.header_name.N the index starts at 1
for(var i = 1; i <= set_cookie_length; i++) {
// extract value using builtin apigee capabilities (best-practice)
var header = context.getVariable("response.header.set-cookie." + i);
print("[BUILTIN] set-cookie["+i+"]: " + header)
if(header.indexOf(cookie_to_find) !== -1){
var extracted_value = header.split("=")[1];
print("Cookie found at [" + i + "] with value " + extracted_value);
}
}
// When you use arrays the index starts at 0
for(var i = 0; i < set_cookie_length; i++) {
// extract value using using array
print("[ARRAY] set-cookie["+i+"]: " + set_cookie_array[i]);
}
context.setVariable("found-value", extracted_value);
This is the debug output:
This is the javascript stepExecution-stdout:
set-cookie collection: [firstcookie=ABCDE, secondcookie=12345]
set-cookie array: [Ljava.lang.Object;@2d2884d4
set-cookie string: firstcookie=ABCDE, secondcookie=12345
number of set-cookie headers: 2
[BUILTIN] set-cookie[1]: firstcookie=ABCDE
[BUILTIN] set-cookie[2]: secondcookie=12345
Cookie found at [2] with value 12345
[ARRAY] set-cookie[0]: firstcookie=ABCDE
[ARRAY] set-cookie[1]: secondcookie=12345
I hope I have been exhaustive and that this can help you.
Thanks @Giupo. It works for me
Hello,
I have faced this problem for several days, I will try to provide you with as many details as possible so as to help other readers as well.
First of all, I would like to point out that by making a no-route example proxy and adding a policy that adds two "set-cookie" headers in request, you will be able to see in the debug that Apigee merges the two headers.
This is the policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM_set-cookie">
<DisplayName>AM_set-cookie</DisplayName>
<Properties/>
<Add>
<Headers>
<Header name="set-cookie">firstcookie=ABCDE</Header>
</Headers>
</Add>
<Add>
<Headers>
<Header name="set-cookie">secondcookie=12345</Header>
</Headers>
</Add>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
This is the debug output:
Apigee provides basic functionality to iterate these values (doc). As you can read, this is a Read-Only collection. You can use request.header.header_name.values.count to have the collection length and request.header.header_name.N to access to the values.
This is a sample JavaScript script with which you can try out these features.
// This return collection (https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html)
var set_cookie_collection = context.getVariable("response.header.set-cookie.values");
// Returns an array containing all of the elements in this collection.
var set_cookie_array = set_cookie_collection.toArray();
// All the values of a particular header in the request, in the form of a single string.
var set_cookie_string = context.getVariable("response.header.set-cookie.values.string");
// Returns the number of elements in this collection.
var set_cookie_length = context.getVariable("response.header.set-cookie.values.count");
print("set-cookie collection: " + set_cookie_collection);
print("set-cookie array: " + set_cookie_array);
print("set-cookie string: " + set_cookie_string);
print("number of set-cookie headers: " + set_cookie_length);
var cookie_to_find = "secondcookie";
// When you use request.header.header_name.N the index starts at 1
for(var i = 1; i <= set_cookie_length; i++) {
// extract value using builtin apigee capabilities (best-practice)
var header = context.getVariable("response.header.set-cookie." + i);
print("[BUILTIN] set-cookie["+i+"]: " + header)
if(header.indexOf(cookie_to_find) !== -1){
var extracted_value = header.split("=")[1];
print("Cookie found at [" + i + "] with value " + extracted_value);
}
}
// When you use arrays the index starts at 0
for(var i = 0; i < set_cookie_length; i++) {
// extract value using using array
print("[ARRAY] set-cookie["+i+"]: " + set_cookie_array[i]);
}
context.setVariable("found-value", extracted_value);
This is the debug output:
This is the javascript stepExecution-stdout:
set-cookie collection: [firstcookie=ABCDE, secondcookie=12345]
set-cookie array: [Ljava.lang.Object;@2d2884d4
set-cookie string: firstcookie=ABCDE, secondcookie=12345
number of set-cookie headers: 2
[BUILTIN] set-cookie[1]: firstcookie=ABCDE
[BUILTIN] set-cookie[2]: secondcookie=12345
Cookie found at [2] with value 12345
[ARRAY] set-cookie[0]: firstcookie=ABCDE
[ARRAY] set-cookie[1]: secondcookie=12345
I hope I have been exhaustive and that this can help you.
@Giupo Thank you so much for the solution,Wil try this and let you know.
Thanks @Giupo. It works for me