17
Views
9
Comments
How to read Incorrect JSON's status code?

Hi All,

If I enter incorrect JSON format then how to understand it? 

{

    "Errors": [

        "Failed to parse JSON request content."

    ],

    "StatusCode": 400

}

I got this Status Code but then how to read this status code. Because accordingly I want to display validation message.


Thanks


mvp_badge
MVP
Rank: #17

Hi Steve,

Your question lacks detail. 

1. Where do you enter the JSON?

2. Share the JSON

With that extra information someone can probably help you.

Regards,

Daniel



Rank: #20572

Hi

I am passing JSON from POSTMAN

mvp_badge
MVP
Rank: #17

Think about how someone can help you, did you give all information you have? I cannot help with the information you share

Rank: #20572

How to validate JSON format data in outsystems

{

"Id": 0

"fName": "th",

"mno":"1234"


here,

1] closing bracket is missed,

2] , missed after id:0


i am passing the data using POSTMAN




mvp_badge
MVP
Rank: #17

Hi,

Ok still doubting what your point is but if you are asking if OutSystems has a JSON validator built in then the answer is no. It will report you that the JSON is invalid. You could try service center for more error details.

Regards,

Daniel

mvp_badge
MVP
Rank: #17

Maybe you can leverage functionality provided by the following Forge component 

https://www.outsystems.com/forge/component-overview/1948/json-validator

It is for version 10, but should work for version 11 too.

Regards,

Daniel





Rank: #20572

No. Using this oml I can't solve it. If I pass invalid JSON from POSTMAN

and I missed something like quotes, comma, curly bracket or anything then how do I fetch this error code?


mvp_badge
MVP
Rank: #17

Sorry, but still not sure why you want to parse invalid JSON from postman to OutSystems. The only thing you can do to catch the exception that OutSystems generate is to add an exception handler. Then you can examine the error.message for more details. 


Rank: #439

Hello Steave,

@Daniël Kuhlmann is right when he said that OutSystems doesn't have a JSON validator built in. So, there will be no alerts when we send incorrect JSON data. 

But, what I understood from your question is that you want to be able to access the status code message.

In your first post: "...how to read this status code. Because accordingly I want to display validation message. "


So, in your REST configuration, go to the Advanced properties, and set the "On After Response" event action.

You will see that now you have an OnAfterResponse action flow. And also, you will find these new structures (unable to edit). You will be able to access those inside that flow. And the JSON that contains the Errors list and the StatusCode, is serialized into the ResponseText field. Note that the StatusCode also comes in an independent integer field (the first inside HTTPResponse).

So, in the OnAfterResponse action flow you should check if the response comes with an Error (check by the Status code integer). Customized your response if needed. 

In case of Error (usually equal or above 400), customize your behaviour accordingly. 

According to the Error structures that you found after using Postman, you can create the structures that you believe that will hold all the error details. (You may configure these along the way)

Errors holds a text List. 

ErrorsResponse holds an Errors structure and a StatusCode integer.

I believe the key aspect to the answer your question is that you should Deserialize the ResponseText into the ErrorsResponse structure that you have just created.

The action flow may look like this:

Note that, in this flow I only treated the case where the Statuscode was 400. You can create a local variable in this flow. Then I aggregated the List of errors in a single text message, called MESSAGE.

Finally, you should create a new User Exception in order to send your Message. And Raise that Exception when you want inside the OnAfterResponse action . It will allow only one Exception Message Input. Pass your customized MESSAGE local variable. After that, go back to the action that calls the REST API, and drag an Exception Handler to  handle your new user Exception. 

Raise an Exception handler for your New User Exception in the scope of the action that did the call, and do what you want with the Response.

This was what I understood from your Question. I hope it helps. There are many ways to act on HTTP responses. If you need to improve your understanding on the Response, insert a breakpoint on the start of the OnAfterResponse action, and check out the values that the HTTPResponse structure holds. 

Cheers,

Rúben