Fault Handling/Assign Message - Can I set response of the error message based on 'Accept' Header

Not applicable

For the default error messages the API Proxy returns, it honors the Accept Header

I want to get a similar functionality in my Assign Message policy, while I am handling faults.

Should I have two AssignMessage policies with step conditions, or can I do that in one Assign Message policy?

Solved Solved
2 1 1,192
1 ACCEPTED SOLUTION

You have options.

You could use 2 AssignMessage policies, with a condition testing the Accept header on one of them, or on both of them.

<Response>
  ...
  <Step>
    <Name>AssignMessage-XMLFormat</Name>
    <Condition>request.header.accept =| "text/xml"</Condition>
  </Step>
  <Step>
    <Name>AssignMessage-JSONFormat</Name>
    <Condition>request.header.accept =| "application/json"</Condition>
  </Step>
  <Step>
    <Name>AssignMessage-TextPlainFormat</Name>
    <Condition>assignmessage.complete = null</Condition>
  </Step>
  ...
</Response>

And then the AssignMessage would look like this:

<AssignMessage name='AssignMessage-XMLFormat'>
  <!-- <AssignTo createNew='false' transport='http' type='request'></AssignTo> -->
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType='text/xml'
             variablePrefix='%' variableSuffix='#'><response>
<error>true</error>
</response></Payload>
    <StatusCode>400</StatusCode>
    <ReasonPhrase>Bad Request</ReasonPhrase>
  </Set>


  <!-- Set this flow variable to indicate the response has been set -->
  <AssignVariable>
    <Name>assignmessage.complete</Name>
    <Value>true</Value>
  </AssignVariable>


</AssignMessage>

You can see the last thing in that AssignMessage sets a context variable. That should appear in each AssignMessage The flow then tests that context variable to see if any of the prior AssignMessage policies has been run.

Another way to do it is to use a single AssignMessage, and then apply an XMLToJSON policy, or a JSONToXML policy, with a Condition element on that conversion policy. I won't provide the code for that; you should be able to figure that out.

View solution in original post

1 REPLY 1

You have options.

You could use 2 AssignMessage policies, with a condition testing the Accept header on one of them, or on both of them.

<Response>
  ...
  <Step>
    <Name>AssignMessage-XMLFormat</Name>
    <Condition>request.header.accept =| "text/xml"</Condition>
  </Step>
  <Step>
    <Name>AssignMessage-JSONFormat</Name>
    <Condition>request.header.accept =| "application/json"</Condition>
  </Step>
  <Step>
    <Name>AssignMessage-TextPlainFormat</Name>
    <Condition>assignmessage.complete = null</Condition>
  </Step>
  ...
</Response>

And then the AssignMessage would look like this:

<AssignMessage name='AssignMessage-XMLFormat'>
  <!-- <AssignTo createNew='false' transport='http' type='request'></AssignTo> -->
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType='text/xml'
             variablePrefix='%' variableSuffix='#'><response>
<error>true</error>
</response></Payload>
    <StatusCode>400</StatusCode>
    <ReasonPhrase>Bad Request</ReasonPhrase>
  </Set>


  <!-- Set this flow variable to indicate the response has been set -->
  <AssignVariable>
    <Name>assignmessage.complete</Name>
    <Value>true</Value>
  </AssignVariable>


</AssignMessage>

You can see the last thing in that AssignMessage sets a context variable. That should appear in each AssignMessage The flow then tests that context variable to see if any of the prior AssignMessage policies has been run.

Another way to do it is to use a single AssignMessage, and then apply an XMLToJSON policy, or a JSONToXML policy, with a Condition element on that conversion policy. I won't provide the code for that; you should be able to figure that out.