Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Change and Set the HTTP status code Response

Not applicable

How can i change and set the HTTP status Code ? I want to set HTTP status code based on my backend service response. Though my backend service is failed some reason(Say Ex:Invalid Request) and the backend service returns Service Error code, but Proxy returns default 200 Ok status code which i want to change based on status of backend service. how do i achieve that ? or what is best way to do that?

Solved Solved
1 13 15.6K
1 ACCEPTED SOLUTION

HI @Kumaresan Sithambaram

If there is a difference between the successful response message and an error response message, you can use an extract variable policy and extract the values from the back end response and use a Raise Fault policy to raise an error with an appropriate http code when that variable is not null.

<Step>
	<Name>fault-resource-not-found</Name>
        <Condition>(responsePrefix.error != null)</Condition>
</Step>

In the above snippet, responsePrefix.error is the value extracted from the response we got from the backend.

And the fault something like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="fault-missing-info">
    <DisplayName>fault-missing-info</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers>
                <Header name="Content-Type">application/json</Header>
            </Headers>
            <Payload contentType="application/json" variablePrefix="#" variableSuffix="%">
            {
                "errors":[
                    {
                        "code": "400",
                        "userMessage": "Missing Info",
                        "systemMessage": "Missing Info"
                    }
                ]
            } 
        </Payload>
            <StatusCode>400</StatusCode>
            <ReasonPhrase>Bad Request</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

As you see in the Raise fault policy, you can configure the response message, http code, etc via flow variables.

Hope this helps

View solution in original post

13 REPLIES 13