setting http status code using Java script policy

Not applicable

Hi Devs,

I am using a java script policy following a service callout policy in my proxy endpoint. I want to proxy all the http status code including error payload from backend system to request invocator. So I set continueOnError="true" in service callout. The problem I see is that now service callout is proxying all the errored response from backend but the status code is overwritten by 200. I used following code in subsequent java script policy;

I see responseStatusCode is populated properly with status code (404 or 500 etc) sent by backend but none of these 3 methods are overwriting the 200 status code. How could this be achieved?

1)

var responseStatusCode= context.getVariable("calloutResponse.status.code");context.setVariable("response.header.x-target-response-code", responseStatusCode);
2) context.setVariable("response.status.code", responseStatusCode);



3) In Target endpoint in assign message policy;

<Set>
        <Headers>
            <Header name="x-target-response-code">{responseStatusCode}
            </Header>
        </Headers>

Solved Solved
0 5 3,899
1 ACCEPTED SOLUTION

Hmm, maybe there is an easier way to accomplish what you want.

In Apigee Edge, the most mainstream pattern is to use Edge as a proxy to a backend system, by configuring the URL For the backend system as a "target". In this case the response from the backend automatically gets propagated to the calling client, if you do nothing else. In other words there is no need to write code to copy the status code received from the backend, to the response.status.code variable. Edge does that for you.

You are doing something different, it seems. You are calling to the backend with a ServiceCallout policy. The SC policy is intended to allow you to augment the normal standard proxy behavior by calling an additional service. If you want to propagate the status result of a SC to the original caller, you CAN do it by assigning the response.status.code variable from within a JavaScript callout, but... that JS must run in the response flow. I suspect you do not have the JS running in the response flow. If the JS runs in the request flow, and you set response.status.code, then the assignment will "work" but the value you set will be silently overwritten when the response flow starts. This perhaps is what you are observing. But as I said, you shouldn't need to do this, if you configure a regular target.

You also mentioned a "Target endpoint" but ... i don't understand why you would need a target endpoint as well as a servicecallout. There is only one backend system you are calling , correct?

If simply using the normal configuration with a target endpoint (as i described above) does not solve the problem, Maybe you can explain in more detail exactly what you are trying to do, with both the ServiceCallout and the Target, and I'll see if I can explain how to handle things.

View solution in original post

5 REPLIES 5

Hmm, maybe there is an easier way to accomplish what you want.

In Apigee Edge, the most mainstream pattern is to use Edge as a proxy to a backend system, by configuring the URL For the backend system as a "target". In this case the response from the backend automatically gets propagated to the calling client, if you do nothing else. In other words there is no need to write code to copy the status code received from the backend, to the response.status.code variable. Edge does that for you.

You are doing something different, it seems. You are calling to the backend with a ServiceCallout policy. The SC policy is intended to allow you to augment the normal standard proxy behavior by calling an additional service. If you want to propagate the status result of a SC to the original caller, you CAN do it by assigning the response.status.code variable from within a JavaScript callout, but... that JS must run in the response flow. I suspect you do not have the JS running in the response flow. If the JS runs in the request flow, and you set response.status.code, then the assignment will "work" but the value you set will be silently overwritten when the response flow starts. This perhaps is what you are observing. But as I said, you shouldn't need to do this, if you configure a regular target.

You also mentioned a "Target endpoint" but ... i don't understand why you would need a target endpoint as well as a servicecallout. There is only one backend system you are calling , correct?

If simply using the normal configuration with a target endpoint (as i described above) does not solve the problem, Maybe you can explain in more detail exactly what you are trying to do, with both the ServiceCallout and the Target, and I'll see if I can explain how to handle things.

Not applicable

I get your point. I created my flow using service call out instead of target endpoint and want to proceed through this approach.

my bad, the assign message policy is in response flow of proxy endpoint. I am not using target endpoint in my API proxy.I want to try following in assign message policy next;

<Set>
        <StatusCode>{responseStatusCode}</StatusCode>
    </Set>

ok, what you're trying should work, though it is going the long way around.

At the moment I cannot figure out why it would not work.

I just tried this in my own API proxy and it works as expected. I am attaching it here.

Import this into your org and deploy it. Then invoke it like so:

curl -i https://ORG-ENV.apigee.net/long-way-round/t1 

You should see this response:

HTTP/1.1 418 I'm a teapot
Date: Thu, 20 Oct 2016 00:03:32 GMT
Content-Length: 0
Connection: keep-alive
Server: Apigee Router

This one does not use the ServiceCallout, but the ServiceCallout should be irrelevant to the problem I think you are facing which is: how can I assign the response status from within a JavaScript callout.

Take note on where the JS callout is executing: in the Response flow.

Good luck!

long-way-round.zip

You are right, I was using JS in Proxy endpoint in request rule to set response code that's why it was not working. I tried the last approach of using Assign message policy in response rule of proxy endpoint and now the status code is coming fine. Thanks for your input.

could u please explain in detail and provide with code and all?

@ansapg