I'm encountering challenges when trying to set multiple headers with the same name in Apigee using both the Assign Message policy and Javascript.
In the Apigee API proxy, I have a requirement to append multiple headers with the same name, each with different values, to the response. However, I'm facing difficulties in achieving this. Only one instance of the header is included in the response, and subsequent headers seem to overwrite the previous ones.
I attempted to use the Assign Message policy to set multiple headers with different values. However, only one instance of the header was set in the response.
code snippet
<Add>
<Headers>
<Header name="Cookies">Value1</Header>
<Header name="Cookies">Value2</Header>
<Header name="Cookies">Value3</Header>
</Headers>
</Add>
I attempted to use JavaScript policy to achieve this. However, this approach was unsuccessful as it overrides and sets only one header in the response, instead of appending multiple headers with different values as intended.
context.setVariable("response.header.cookies", "Value1");
context.setVariable("response.header.cookies", "Value2");
context.setVariable("response.header.cookies", "Value3");
Question: I expect to have multiple headers with the same name included in the response. Is there a way we can achieve this in Apigee? @dino @dchiesa1 @sidd-harth
Yes you can set a variable like this
<AssignMessage name='AM-1'>
<AssignVariable>
<Name>response.header.cookies.1</Name>
<Template>whatever</Template>
</AssignVariable>
</AssignMessage>
check this documentation
https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#response
the requirement is to set the response headers like in the above image.
But the flow variable response.header.header_name.N sets the response like in the image below
JS Code snippet
context.setVariable("response.header.cookies.1", "april");
context.setVariable("response.header.cookies.2", "may");
context.setVariable("response.header.cookies.3", "june");
Feel free to correct me if my understanding is mistaken
@playarun93 wrote:
Feel free to correct me if my understanding is mistaken
Yes,
If you have a multi-valued header, HTTP says you should be able to concatenate them with commas, and it's equivalent.
https://stackoverflow.com/a/39055745
So the two cases you are describing, are the same. If there is a system that insists on one versus the other, that system is broken. Not complying with the HTTP spec. and that system needs to be fixed.