Dynamically set Headers in Response

Not applicable

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 ?

Solved Solved
2 7 3,004
1 ACCEPTED SOLUTION

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?

View solution in original post

7 REPLIES 7

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?

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 🙂

@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.

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.

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