Get the HTTP status code returned by a failed consumed REST web service


I have this scenario where I need to handle errors returned by a consumed REST API differently, according to the returned HTTP status code. What this help article suggests doesn't give me access to that code, so I'm looking for another way.

I've noticed I can add a OnAfterResponse handler to my web service and have access to the status code there. Now, what would be a good way to transfer it back from there to the action that invoked the web service?

I think a good approach is to return the JSon response with an "error" field with the answer (error code) You have to add I (the error field) to your structure and always test this field. 

This way, the changes in code would be minimal and you would have the code available to the action calling the rest web service.

Hi Jauch.

I actually didn't get your approach.

Can you show an example or at least name the actions/events where you would perform those steps for a consumed rest Api?




Hi Gonçalo,

The idea is the same as when you have a response that returns a variable field type depending on the request.

Example. The CTT web service that return the postal code for an address.

Depending of the address you send in the request, the field returned for the postal code can assume 3 forms.

1. It is not there at all

2. It is a single value

3. It is a list of values

This causes errors when the system tries to translate the json structure into the OutSystems structure.

You have to "normalize" the Json answer before the translation. What we do is to change the field to a list if it is a single value, manipulating the answer (that is pure text), in the OnAfterResponse action.

So, the idea here is to do something similar.

Add a new field to the structure (OutSystems side), that will receive the value of the http status.

In the OnAfterResponse, add the code, inserting the field into the json response manipulating the string.

This way the code will be in the response and will be translated, becoming available in the OutSystems application automatically.


you can always set the statuscode (check out httprequesthandler).

(however some are reserved and will dump you the default error screen like 401)

@J I think Joao's question is how to capture the statuscode when a REST service returns one, not how to set it when you're writing the REST service (in which case you're right).

@Joao, I think that, though Jauch is on to something, it would be easier adding an element to the Headers list attribute of the OnAfterResponse's Response structure, rather than muck with the JSON.

While conceptually I prefer to have the return code in the response itself, if it is easier to just add a header info and retrieve it on the other side, probably I would go for it also.

Let me get this straight.

You have Action "Foo"

which calls an webservice "Bar" that will return some response and a statuscode.

in the action "Foo",  you want to return the statuscode somehow?

I think the response-altering should be indeed the way to go, altering the headers seems to me a overkill.

(and tbh, in outsystems you should be able to get the statuscode more easily)

J, to add the statuscode to the Response.Headers is as simple as a ListAppend, while altering the response means you need to parse the JSON. I really can't see why a simple ListAppend as opposed to parsing JSON is "Overkill"? Especially since at the consuming side, you can just add in Output parameter of type Header...

(Stay tuned for "battle of the MVPs", now coming to your local forum...)

The overkill is more (in both cases) of something you have to artificially add while it should be available as is.

(we can lock it now :P )

There is no need to parse the json. The response is text, you can insert the pair property value very easily using many different ways.

Sure, but nothing is easier than a ListAppend :).

In fact, no :)

P.S. I was agreeing with you ;) lol

Let's agree to disagree then :).

I did a quick test and it seems to work fine.

And personally, I also prefer the ListAppend approach   

Thanks for all the replies!

Just for the record, adding the status code to the response text is a viable solution, although I don't like the idea of changing the response in order to include this.

If I'm not misunderstanding, Kilian's proposed solution (adding a HTTP Header) doesn't seem to be a viable solution, because I would be adding it to a request that ended and to which i don't have access from the action that invoked the web service method.