How to get response code of a REST API post method

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.