REST Status Code 400 response handling

0
Hi, I'm calling a REST service which usually results in a successfull response (Code 200) and I do the JSON mapping - everything is fine   However there are cases the input is insufficient or invalid. I cannot validate all casses in that matter. When the REST ist called with bad data, the service responds with a status code 400 and a response body containing a message like: { "message": "You fool did something wrong" } The REST service is not under my control, I need to handle the 400 response. Or even other 4xx codes   I tried to change the response handling to store the HTTP response in a System.HttpResponseSystem object.  But it still dumps and says 'Internal server error'   In the log I can see the REST call was responded with status code 400, and the above mentioned message body.   This still works when the return code is 200, I can take the body and call the JSON mapping successfully. Is it possible to handle status code 400 /4xx ?  
asked
3 answers
0

Mendix has been designed the "Call Rest"-activity to throw an error upon receiving a http-response with statuscode >= 400. So you will have to handle that error like this:

image.png

In the true-path, you will have the latetst http-response, hopefully containing a useful message.

If that message is not handing you useful information, try to get their detailed api-definition, try to get into contact with a person handling the api. If that all fails, you can only store the request and response and hope that you can derive their logic someday.

answered
0

Hi,

yes that does the trick of 'catching' the response error, thank you.

Before accepting this answer, can you elaborate whether I missed something when dealing with the response: I cannot find the response body, which contains the error message by the service. Here is the console log of the HTTP response

image.png

you can see there is a JSON body object with a 'message'

however in the debugger I expected the body to be in the content, but it is empty

 

image.png

 

kind regards

Stefan

answered
0

Hi,

I just figured out that the 'Consume REST service' action is indeed able to process the response body when status code is 400:

 

image.png

I will hence use the new artifact and call a JSON import mapper for the response body to provide the entity which I can then display as an error.

Thank you very much

Stefan

answered