Error in Status Code while preparing Response with custom headers

Not applicable

I have custom Assign message policy which returns custom headers in response with createNew=true in policy, but the issue is it always returns 200 Ok status code even though the backend returns 204, 201.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="Remove-Response-Headers">
    <DisplayName>Remove Response Headers</DisplayName>
    <Copy source="response">
        <Headers>
            <Header name="Accept"/>
            <Header name="Content-Type"/>
            <Header name="Connection"/>
            <Header name="Access-Control-Allow-Origin"/>
            <Header name="Access-Control-Allow-Methods"/>
        </Headers>
    </Copy>
    <Set>
        <Payload variablePrefix="$" variableSuffix="#">$response.content#</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="response"/>
</AssignMessage>

How do I fix this? I need to set the response code to be same as returned from backend. I tried copying, setting and using assignVariable tag but nothing seems to work!!.

Note we have to create new response as we have to remove all unnecessary headers and copy only certain headers in response.

Please guide!!

Solved Solved
1 2 634
2 ACCEPTED SOLUTIONS

rovyas
Participant I

Hi,

Just add StatusCode tag in your Copy section with true as its value, it will reflect status of your target

 <Copy source="response">
        <Headers>
            <Header name="Accept"/>
            <Header name="Content-Type"/>
            <Header name="Connection"/>
            <Header name="Access-Control-Allow-Origin"/>
            <Header name="Access-Control-Allow-Methods"/>
        </Headers>
        <StatusCode>true</StatusCode>
    </Copy>

View solution in original post

In your AssignMessage policy, you are using createNew=true, but you are not setting the status code. So you get the default, which I believe is 200 OK.

The way to fix this is to explicitly set the status code. Your policy configuration might look like this:

<AssignMessage name="Remove-Response-Headers">
    <DisplayName>Remove Response Headers</DisplayName>
    <Copy source="response">
        <Headers>
            <Header name="Accept"/>
            <Header name="Content-Type"/>
            <Header name="Connection"/>
            <Header name="Access-Control-Allow-Origin"/>
            <Header name="Access-Control-Allow-Methods"/>
        </Headers>
    </Copy>
    <Set>
        <Payload>{response.content}</Payload>
        <StatusCode>{response.status.code}</StatusCode>
        <ReasonPhrase>{response.reason.phrase}</ReasonPhrase>
    </Set>
    <AssignTo createNew="true" transport="http" type="response"/>
</AssignMessage>

The StatusCode, ReasonPhrase, and Payload elements each accept what is known as a "message template". This means a variable name wrapped in curlies will be replaced at runtime with the value of the referenced variable.

View solution in original post

2 REPLIES 2

rovyas
Participant I

Hi,

Just add StatusCode tag in your Copy section with true as its value, it will reflect status of your target

 <Copy source="response">
        <Headers>
            <Header name="Accept"/>
            <Header name="Content-Type"/>
            <Header name="Connection"/>
            <Header name="Access-Control-Allow-Origin"/>
            <Header name="Access-Control-Allow-Methods"/>
        </Headers>
        <StatusCode>true</StatusCode>
    </Copy>

In your AssignMessage policy, you are using createNew=true, but you are not setting the status code. So you get the default, which I believe is 200 OK.

The way to fix this is to explicitly set the status code. Your policy configuration might look like this:

<AssignMessage name="Remove-Response-Headers">
    <DisplayName>Remove Response Headers</DisplayName>
    <Copy source="response">
        <Headers>
            <Header name="Accept"/>
            <Header name="Content-Type"/>
            <Header name="Connection"/>
            <Header name="Access-Control-Allow-Origin"/>
            <Header name="Access-Control-Allow-Methods"/>
        </Headers>
    </Copy>
    <Set>
        <Payload>{response.content}</Payload>
        <StatusCode>{response.status.code}</StatusCode>
        <ReasonPhrase>{response.reason.phrase}</ReasonPhrase>
    </Set>
    <AssignTo createNew="true" transport="http" type="response"/>
</AssignMessage>

The StatusCode, ReasonPhrase, and Payload elements each accept what is known as a "message template". This means a variable name wrapped in curlies will be replaced at runtime with the value of the referenced variable.