Hi,
I'm calling a REST API method (where HTTP method = POST) in a server action. When this method gets executed, on successful execution it should return 200 as status code. Can you please suggest how can I get the status code? Based on the status code returned the other actions will take place.
Thanks in advance.
Biplab
Hi Biplab,
Yes you can able to get the status code, in the Consumed Rest API use "OnAfterResponse" event,
And inside this action you can customize your response, in the Response input structure you have "StatusCode" and "ResponseText" etc.., in ResponseText you have Response Json data, here you can add this statuscode in json structure.
Note: Your original response must be also have this new structure, so change that also before calling.
After changing json data locally,assign it in your CustomizedResponse, so you will get your StatusCode in your output data.
Thanks,
Balu
Balu wrote:
Hi Balu,
Thanks for your reply.
Could you please elaborate the below part as mentioned in your response:
"And inside this action you can customize your response, in the Response input structure you have "StatusCode" and "ResponseText" etc.., in ResponseText you have Response Json data, here you can add this statuscode in json structure."
Do I need to add statuscode in Response.ResponseText? This is non-editable, how to add, please suggest.
I'm using action where I'm calling the REST API (TestAPI) see below please. From this action how can I access the CustomizedResponse.StatusCode? In this below flow I need to add If condition to check the status value.
The status code is unfortunately not directly available. Like Balu said, you need to add an OnAfterResponse Action to inspect it. It's available as the StatusCode Attribute of the HTTPResponse Structure (Input Parameter "Response" of the OnAfterResponse).
Since the OnAfterResponse is "global" to the REST API (so the same OnAfterResponse is called for all the APIs Methods), you need to pass it to specific Method. There are two options for that. One is suggested by Balu, and entails extending the response Structure of the specific Method with a status code Attribute, and setting that. The downside to this is that 1) you need to deserialize and serialize the JSON received and 2) if you have multiple Methods with different Structures, you need to detect which Method has been called, which is non-trivial.
An easier approach would be to use a Session Variable. Declare a Session Variable, e.g. "RESTStatusCode" of type Integer, and in the OnAfterResponse assign Response.StatusCode to Session.RESTStatusCode, and set the Response.StatusCode to 200 to prevent an Exception. Next, in the Action you're calling the REST Method you can check the Response.StatusCode.
In the OnAfterResponse you can have Response.ResponseText, and assign this value into local variable, and add new node with json format like this, in your localvaraible, based on your usability,
{ "statuscode":"200"}
After assign this new json data (local variable) to output variable of CustomizedResponse.ResponseText of OnAfterResponse,
Then you will get RestAPI with response code(here you need to change the output structure to accept "statuscode", otherwise will give json error.
Could you please help on my below question.
I'm getting the below:
Response.ResponseText = "{""name"":""ABC DE 12345""}"
I have created a new local variable say. locval and assigned the value.
locval = "{""name"":""ABC DE 12345""}"
Now how to add the add the new node with json format like this, in this localvaraible,
and what changes I need to do to change the output structure to accept "statuscode"
Thanks a lot.
You seem to completely ignore my response above. Any reason you don't want to go for the easiest solution?
Kilian Hekhuis wrote:
Hi Kilian,
Thanks you for providing the alternate solution. Actually I would like to check for the other solution as well as this I never tried before and also to get more understanding on the flow.
Hello Biplap,
What is wrong with the answer given here: https://www.outsystems.com/forums/discussion/41494/json-parse-and-append/?AND with the answer Killian gave to you in this topic, that is the same: Deserialize the JSON to a structure, put the value, serialize again and let the service took their path? You can always try to mess with the JSON string directly, but this is user much more complicated and error prone :)But Killian's solution with the Session variable and using the status code in the structure existent is a best approach.
Just thought I would share the solution I arrived at for this when using a service module (which does not have session variables available) and also avoids de-serializing / serializing the JSON to manipulate the response.
Here is a screenshot that shows my OnAfterResponse logic (I am checking for 5xx status codes in this case):
Hi Brian,
You start by saying "[this] avoids de-serializing", but then you propose to use ArdoJSON's JSON2RecordList, which is exactly that: deserializing. Also, that ArdoJSON Action has been deprecated for years now, just use the built-in JSON Deserialize Statement if you want to deserialize JSON. I also fail to see why you would want to deserialize the body, as you're only manipulating the header?
Kilian,
By "avoids de-serializing / serializing the JSON to manipulate the response" I meant that you do not need to do this to manipulate the response body - a solution proposed by Balu which you touched on. So the response JSON structure remains the same.
I am only de-serializing the response here to easily read a value from it which I then use in my new HTTP header value (4.4 in my answer). Because I know the error JSON structure for 4xx/5xx status codes, I can do this. This all depends on the API of course.
As for using ArdoJSON vs. the built-in JSON de-serialize - yes you are correct. In this environment the ArdoJSON was being used so I just continued with it not thinking about the built-in action but yes that should be used.
In order to get the HTTP status Code in the Rest API response, we are required to provide the handler for OnAfterResponse or OnAfterResponseAdvanced. Here in the body of the handler, we are required to update the CustomizedResponseText.
We are required to set the below properties here:
- CustomizedResponse.ResponseText
- CustomizedResponse.StatusCode
- CustomizedResponse.StatusLine
- CustomizedResponse.Headers
- CustomizedResponse.ResponseBinary
In order to get the status code, we need to pass the custom json as mentioned below:
"{" + """StatusCode""" + ":" + Response.StatusCode + ", " + """RecordsList""" + ":" + Response.ResponseText + "}"
Here Response.ResponseText is the actual response which we get from the Rest API.
In order to parse this JSON, we need to create a custom Response Structure accordingly and we need to update the datatype for the API Response property.