Dynamically set Headers in Response

I have the following requirements:

  1. Set in Proxy KVM the NAME of the headers to be send in the response to the client.

  2. Read from KVM and dynamically set the headers in the response.

Any idea how this can be achieved ?

2 Likes

Use a single KVM entry, with a JSON object of headers. E.g.

Key: Headers

Value:

{

“Content-Type”:“application/json”,

“CustomHeader”:“abc”

}

Then use javascript to parse this object…

var kvmHeaders = JSON.parse(context.getVariable("kvmHeaders")); //read from KVM

for(prop in kvmHeaders) {    

	context.setVariable("response.header."+prop, kvmHeaders[prop]);

}

How does that sound?

3 Likes

That sounds like a really good answer to the question he asked!

But, would the kvmHeaders var need to get the value of the JSON.parse() ? I think so.

Good spot - correcting now :slight_smile:

@Dino, @Sean Davis,

Thanks for the help.

But the requirement was slightly different.

We need to do the following:

  1. Read from KVM the eligible header name

  2. If the request header’s name is matching with the KVM entry, send the header to the Backend with the same value.

  3. If the backend response header’s name is matching with the KVM entry, send the header to the client with the same value.

Any help would be appreciated.

Answer provided by @seanadavis is nice one.

Another option is to use the Message Policy and manipulate the

<Payload> 

http://docs.apigee.com/api-services/reference/assign-message-policy

I used for Remove headers and it worked perfectly.

Best Regards

Sujith Mathew

So you would like to remove all headers, except those on a list of ‘allowed’ request/response headers?

Yes. And the name of the allowed should be picked up from KVM.