AssignMessage - How can I get all headers ?

ayheber
Participant I

I'm using AssignMessage to generate a new HTTP request, and I want to put all headers of the original request in the payload of the new request.

From what I see in the variables-reference, I can only refer to a specific header (request.header.header_name.values).

How can I get a full list of all headers and their values?

Solved Solved
0 5 2,312
1 ACCEPTED SOLUTION

There is no way to read a single variable that contains, for example, all of the names and values of the inbound headers in a JSON format. If you want to embed the names and values of the inbound request headers into the payload of a new, different request, then you probably want to use a JavaScript callout to do so.

If I use this JavaScript in the response flow:

var rh = context.getVariable('request.headers.names');
context.proxyResponse.content = String(rh);

...then when invoking the API Proxy I see this result:

$ curl -i https://$ORG-$ENV.apigee.net/jstest-1/t1
HTTP/1.1 200 OK
Date: Tue, 21 Jan 2020 22:42:45 GMT
Content-Type: text/plain
Content-Length: 80
Connection: keep-alive


[Accept, Host, User-Agent, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto]


What you're seeing there is the response payload contains a stringified version of the list of request header NAMES.

If instead I use this Javascript:

var rh = context.getVariable('request.headers.names');
var s = String(rh);
// convert the list of header names to an array:
var list = s.substring(1, s.length - 1).split(new RegExp(', ', 'g'));
var hash = {};
list.forEach(function(headerName) {
  hash[headerName] = context.getVariable('request.header.' + headerName);
});
context.proxyResponse.content = JSON.stringify(hash, null, 2);

Then the response payload is more like this:

{
    "Accept": "*/*",
    "Host": "gaccelerate3-test.apigee.net",
    "User-Agent": "curl/7.61.1",
    "X-Forwarded-For": "54.14.13.16",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
}

AFAIK you cannot do that with AssignMessage.

View solution in original post

5 REPLIES 5

jaupadhyay
Participant IV

Hi @Ari Heber

Please refer to following section of Assign message documentation page.

https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#assignto


In your AssignMessage Policy under AssignTo section if you configure it correctly (createNew attibute value is set to false) you don't need to copy headers from original request and it will use the headers form source request and pass it on to target back end.Following line in your assign message policy should do that.


<AssignTo createNew="false" transport="http" type="request"/>

Thanks Jayesh.

I am trying to put the headers of the original request in the payload of the new request. I do not want to use them as "headers" of the new request.

Is there a way to get it as an array ?

There is no way to read a single variable that contains, for example, all of the names and values of the inbound headers in a JSON format. If you want to embed the names and values of the inbound request headers into the payload of a new, different request, then you probably want to use a JavaScript callout to do so.

If I use this JavaScript in the response flow:

var rh = context.getVariable('request.headers.names');
context.proxyResponse.content = String(rh);

...then when invoking the API Proxy I see this result:

$ curl -i https://$ORG-$ENV.apigee.net/jstest-1/t1
HTTP/1.1 200 OK
Date: Tue, 21 Jan 2020 22:42:45 GMT
Content-Type: text/plain
Content-Length: 80
Connection: keep-alive


[Accept, Host, User-Agent, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto]


What you're seeing there is the response payload contains a stringified version of the list of request header NAMES.

If instead I use this Javascript:

var rh = context.getVariable('request.headers.names');
var s = String(rh);
// convert the list of header names to an array:
var list = s.substring(1, s.length - 1).split(new RegExp(', ', 'g'));
var hash = {};
list.forEach(function(headerName) {
  hash[headerName] = context.getVariable('request.header.' + headerName);
});
context.proxyResponse.content = JSON.stringify(hash, null, 2);

Then the response payload is more like this:

{
    "Accept": "*/*",
    "Host": "gaccelerate3-test.apigee.net",
    "User-Agent": "curl/7.61.1",
    "X-Forwarded-For": "54.14.13.16",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
}

AFAIK you cannot do that with AssignMessage.

Thanks alot @Dino-at-Google.

Please note that your code is ignoring multi-values headers.

Yes, that is correct. To handle Multi-valued headers you'll have to add a little bit more logic.