Hi everyone,
I'm working on a Server Action in OutSystems ODC where I’m using an HTTP Request to call an external API. What I would like to achieve is to retrieve the HTTP response code (e.g. 200, 400, 500, etc.) within the same Server Action, so I can use it in an If condition and control the rest of the logic based on that code.
For example:
If the response is 200 OK, I want to update the status of the related record to "Completed".
If it’s something like 400 Bad Request, I want to set the status to "Error" for that specific record.
I’m aware of the OnAfterResponse event, but the issue is that in that context I don’t have access to the original record or data that triggered the HTTP Request.
That makes it very hard to link the response code back to the specific record that was sent, which is critical in my use case.
I’ve tried to find a way to get the response code directly inside the Server Action after the HTTP Request, but I can’t seem to find any output or property that gives me that information.
So my main question is: Is there a way to get the HTTP response code from the HTTP Request directly within the Server Action, so I can immediately use it in logic like If statements and status updates?
Any help, examples or suggestions would be greatly appreciated.
Thanks in advance!
Kind regards, Piet Pieper
Hi Piet,
Assuming that ODC works the same as O11 (which I'm more familiar with), if you receive anything other than a 200-series status code the Platform will trigger an exception. To avoid this, you must use the On After Response to check if the status code is in the 200 range, and if not set it to 200.
To pass the original status code back to the caller, you could add an output header parameter to the method, e.g. "StatusCode", with a name in the request of "X-StatusCode", and set it in the On After Response to the value of the original status code. Thus you can avoid an exception, and handle the original status code.
For an example on how handle and pass statuscodes you can install one of my "Graph" connector libraries on ODC Forge (valantic LCS). At a glance in the OnAfterResponse the status code is checked and on a status code >= 400 serializes an error structure containing the status code and raises an exception. This exception is then handled in the calling server action.
Best
Stefan