How can I set mutliple values to a Header in RaiseFault policy ?

In a raise-fault policy I am setting the following:


<Header name="Access-Control-Allow-Headers">{request.header.Access-Control-Request-Headers}</Header>

The “Access-Control-Request-Headers” has multiple values, such as “content-type, authorization”. To my surprise, “Access-Control-Allow-Headers” contains just the first part of the multiple values (“content-type” in the above example).

How can I assign a header in response to the exact same value as a header in request?

Solved Solved
1 4 782
1 ACCEPTED SOLUTION

Hey Amar, you may want to consider -

You can refer to a variable named like this: request.header.HEADERNAME.values .

This holds all of the values of the header, in an array. Check the variables reference for more on that.

If you refer to that variable in a context in which a string is expected, then the value gets resolved as a comma-separated list, surrounded by square brackets, like this:

[ value1, value2 ] 

Using the new static functions for message templates (overview here), you can use a substring to remove the brackets. This eliminates the need for the JS step.

This works for me

<AssignMessage name='AM-Response'>
  <Set>
    <Headers>
      <Header name='LoopbackHeader'>{substring(request.header.testheader.values,1,-1)}</Header>
    </Headers>
    <Payload contentType='application/json'>{
    "status" : "ok"
}</Payload>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew='false' transport='http' type='response'/>
</AssignMessage>

Invoking that policy with a request message that includes 2 values for test-header, gives me the expected value in the response header.

The idea behind supporting static functions within message templates was to eliminate the need to call out to JavaScript for simple common operations, like substring.

View solution in original post

4 REPLIES 4

When we try to retrieve the values of a header which has multiple values, you get only the first value. If you need to retrieve all the values of a multi value header, then you need to do this in a different way.


Here's one way that I have found:

You can use JavaScript Policy to retrieve all the values of the multi value header and then set those values to a new variable. Subsequently use this new variable in your RaiseFault Policy.

1. Here's the JavaScript Policy code (let's JS-ReadMultiValueHeader.js)

// Retrieve all the values for the header
// Note: the result will have square brackets. For ex: [value1, value2]
var headerValues = context.getVariable("request.header.Access-Control-Request-Headers.values") + '';
print("Header Values = ", headerValues);
// get the array of header values by removing the square brackets
var strHeaderValues = headerValues.substring(1, headerValues.length-1).split(',');
print("strHeaderValues = " + strHeaderValues);
// set the values without square brackets to another variable
context.setVariable("accessControlRequestHeader", strHeaderValues.toString());

2. Now use this new variable in your RaiseFault policy as follows:
<Headers> 
<Header name="Access-Control-Allow-Headers">{accessControlRequestHeader}</Header> 
</Headers>

With this I was able to set the header with multiple values in RaiseFault policy.

Hey Amar, you may want to consider -

You can refer to a variable named like this: request.header.HEADERNAME.values .

This holds all of the values of the header, in an array. Check the variables reference for more on that.

If you refer to that variable in a context in which a string is expected, then the value gets resolved as a comma-separated list, surrounded by square brackets, like this:

[ value1, value2 ] 

Using the new static functions for message templates (overview here), you can use a substring to remove the brackets. This eliminates the need for the JS step.

This works for me

<AssignMessage name='AM-Response'>
  <Set>
    <Headers>
      <Header name='LoopbackHeader'>{substring(request.header.testheader.values,1,-1)}</Header>
    </Headers>
    <Payload contentType='application/json'>{
    "status" : "ok"
}</Payload>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew='false' transport='http' type='response'/>
</AssignMessage>

Invoking that policy with a request message that includes 2 values for test-header, gives me the expected value in the response header.

The idea behind supporting static functions within message templates was to eliminate the need to call out to JavaScript for simple common operations, like substring.

@Dino-at-Google,

Thank you very much for sharing this info. I was not aware of that we could use substring in AssignMessage policy.

Will try it out and update.

@Dino-at-Google,

This worked and is a much easier solution to just use the substring function within the RaiseFault policy. Thanks for sharing this solution.