How can I use a single policy to set some common headers in response or error flows ?

Sometimes the APIs are successful, so the execution flow goes through the response flow. Sometimes the APIs fail and the execution flow goes through the error flow. I would like to set some headers for ex: region from which the request came, request id, elapsed time etc using a single policy (to avoid redundant policies) irrespective of whether I am in response flow or error flow. I tried to use response.header but the headers don’t get displayed if the execution of the API goes through the error flow. This is because response object is not available in the error flow.

Is there some way to achieve this ?

Solved Solved
1 1 397
1 ACCEPTED SOLUTION

After some experiments, I have figured out the following ways through which we can achieve this:

  • Use AssignMessage Policy to set the headers as shown below:
  • <Set>
        <Headers>
            <Header name=“X-Request-Region”>{system.region.name}</Header>
            <Header name="X-Request-Id">{messageid}</Header> 
            <Header name="X-Request-Duration">{requestDuration}</Header>
        </Headers>
    </Set>
    

    Note: The AssignMessage policy knows the context flow and automatically sets the headers in the right object. That is, if we are in the response flow, then it sets the header in the response object. Similarly, if we are in the error/fault flow, it sets the headers in the error object.

    OR

  • You can also use a single Javascript policy with the use of message object.
  • var startTime = context.getVariable("client.received.start.timestamp");
    var endTime = context.getVariable("system.timestamp");
    var requestDuration = (endTime - startTime).toString();
    context.setVariable("message.header.X.Request-Region", context.getVariable("system.region.name");
    context.setVariable("message.header.X-Request-Id", context.getVariable("messageid"));
    context.setVariable("message.header.X.Request-Duration", requestDuration);
    

    Note: message is a contextual object, with the same value as request in the request Flow or as response in the response Flow or as error in the Error flow. So using message.header.{headername}, you can set the values in the right place irrespective of which flow the API is executing. You can read more about this here.

    View solution in original post

    1 REPLY 1

    After some experiments, I have figured out the following ways through which we can achieve this:

  • Use AssignMessage Policy to set the headers as shown below:
  • <Set>
        <Headers>
            <Header name=“X-Request-Region”>{system.region.name}</Header>
            <Header name="X-Request-Id">{messageid}</Header> 
            <Header name="X-Request-Duration">{requestDuration}</Header>
        </Headers>
    </Set>
    

    Note: The AssignMessage policy knows the context flow and automatically sets the headers in the right object. That is, if we are in the response flow, then it sets the header in the response object. Similarly, if we are in the error/fault flow, it sets the headers in the error object.

    OR

  • You can also use a single Javascript policy with the use of message object.
  • var startTime = context.getVariable("client.received.start.timestamp");
    var endTime = context.getVariable("system.timestamp");
    var requestDuration = (endTime - startTime).toString();
    context.setVariable("message.header.X.Request-Region", context.getVariable("system.region.name");
    context.setVariable("message.header.X-Request-Id", context.getVariable("messageid"));
    context.setVariable("message.header.X.Request-Duration", requestDuration);
    

    Note: message is a contextual object, with the same value as request in the request Flow or as response in the response Flow or as error in the Error flow. So using message.header.{headername}, you can set the values in the right place irrespective of which flow the API is executing. You can read more about this here.