How to get response code of a REST API post method

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

Hi Biplab,

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.

Balu wrote:

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


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.

Thanks,

Biplab

Hi,

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.


Thanks,

Balu

Hi Biplab,

You seem to completely ignore my response above. Any reason you don't want to go for the easiest solution?

Kilian Hekhuis wrote:

Hi Biplab,

You seem to completely ignore my response above. Any reason you don't want to go for the easiest solution?


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.


Thanks,

Biplab

Balu wrote:

Hi,

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.


Thanks,

Balu

Hi Balu,

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,

{
   "statuscode":"200"
}


and what changes I need to do to change the output structure to accept "statuscode"


Thanks a lot.


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.

  1. Add a new output parameter to your API method to store the status code, error message, etc.
  2. Set the output parameter as a header ("Receive In" property = Header)
  3. Give the parameter a unique name (ex: "myapp-error" or "myapp-statuscode") and a default value of an empty string ("") or 0 if an int.
  4. In the OnAfterResponse action:
    1. Add an HTTPHeader local variable
    2. Optionally log the original Response.ResponseText if you wish to preserve this.
    3. Read the Response.StatusCode and if a 4xx/5xx/etc. status code parse the Response.ResponseText to a structure that maps the JSON using ArdoJSON's JSON2RecordList.
    4. Set the HTTPHeader with a name matching the output parameter(s) added above and the value you wish to retrieve (error message, status code, etc.)
    5. Append the HTTPHeader variable to the CustomizedResponse.Headers list.
    6. Set the CustomizedResponse.StatusCode to a 200.
  5. In your logic where you call the API, check the output parameter(s) added above.

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.