Parsing error when consuming REST API on mobile APP

I am a beginner for outsystems 11 and trying to consume REST API for displaying data on my APP.


But I have some problems. 

I use consume REST API and finish settings on popup dialog which including: 

  1. Fill in Method URL. 
  2. Fill in Content-type:application/json.
  3. Basic Authentication, fill in account and password.
  4. Click TEST on Test tab,
  5. Click copy to response body and Finish.
  6. Rest API Method Name will be “GetIndex”.

Back to the interface, add the new screen on the Main flow

  1.  Add fetch data from other Source of data action
  2. Change the data type of output parameter(Out1),
  3. Pull the last step we get REST API Method to data action
  4. Add new Assign, Out1= GetIndex.Response.Current


Publish APP version and open it in the browser.

It will display an error message when launching app 

Failed to parse response of the method 'GetIndex' of the 'NookTestrail' REST API: Parsing '': Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


It confuses me because I can test correctly on consuming REST API dialog. But fail to use data action.


Attachment is my json output data getting from REST API dialog.


Ps. 

  1. In order to check the log, I have referred to some articles like https://www.outsystems.com/forums/discussion/33272/setting-logging-level-of-a-rest-api/. But I still can not get more information.


Any ideas on how to resolve this issue?

The error is happening when parsing the JSON response to an OutSystems structure.

I'd say that either your response structure is missing something or you are receiving something else in the response body that OutSystems is not expecting. 

Ruben Bernardo wrote:

The error is happening when parsing the JSON response to an OutSystems structure.

I'd say that either your response structure is missing something or you are receiving something else in the response body that OutSystems is not expecting. 


Is there any way to check JSON raw data to know what structure is wrong?

Hi Aron,

First, the error you are receiving means you get served an HTML error page instead of a proper JSON response. This can be caused by various problems, but a recent case has found that some APIs don't like the "charset=utf-8" that the Platform adds to the Content-type, but _only_ when the program is running, not when testing in Service Studio. If this is indeed the cause, you can fix this as is described in the linked post.

EDIT: To check the raw HTTP trace, see my post here.

Kilian Hekhuis wrote:

Hi Aron,

First, the error you are receiving means you get served an HTML error page instead of a proper JSON response. This can be caused by various problems, but a recent case has found that some APIs don't like the "charset=utf-8" that the Platform adds to the Content-type, but _only_ when the program is running, not when testing in Service Studio. If this is indeed the cause, you can fix this as is described in the linked post.

EDIT: To check the raw HTTP trace, see my post here.


Hi Kilian Hekhuis

Thanks for your reply. I checked HTTP trace and found something wrong. 

The GET URL I enter is "https://nook.testrail.com/index.php?/api/v2/get_runs/45 ". But the return of HTTP request on GET is different from my expectation.

Expect a return on HTTP request: ---> GET /index.php?/api/v2/get_runs/45 HTTP/1.1

Actual return on HTTP trace:   -----> GET https://nook.testrail.com/index.php HTTP/1.1


How to fix it?



Hi Aron,

Unfortunately I don't understand what you mean. The REST HTTP trace shows the following information:

The first line is the actual HTTP get the platform performs. It's the full URL, followed by "HTTP/1.1". The second line is the User-Agent (always "OutSystemsPlatform"), followed by any header parameters you have specified. After the two blank lines the respons of the other server is displayed. It'll start with a "HTTP/1.1 200 OK" in case everything is ok, or some error code if it's not, followed by other header information. Then a blank line, and then the body, which in most cases will be JSON.

Can you explain in more detail what goes wrong in your case?

Hi Kilian Hekhuis

The GET URL I input

Below is http trace:


Compare to http trace, it is totally different I want. There is no string "/api/v2/get_runs/45".

So I can not get the response I want.

But I test on REST API dialog, I can get the correct response.

Is there any information I need to provide?


Aron,

I did a few tests and the problem seems to be related with the method's URL path. When appending the URL path to the Base URL, OutSystems ignores everything that comes after "?" character maybe because the platform uses this character right before sending the input parameters for the GET method.

Instead of calling https://nook.testrail.com/index.php?/api/v2/get_runs/45, you are calling just https://nook.testrail.com/index.php? /api/v2/get_runs/45, hence the error you are obtaining.

My first suggestion is to remove the question mark out of the endpoint and repeat the process. The endpoint would look like this: https://nook.testrail.com/index.php/api/v2/get_runs/45.

If that doesn't work you could try a different approach, although not recommended. Basically edit the Method's URL Path by replacing the question mark by %3F. Then use OnBeforeRequest to replace it by the question mark.

Hope this helps you solve the problem.

Hi Ruben Bernardo


Thanks for your reply. I try to modify the URL and follow your suggestion. It did not work, it would not be the correct URL path for this API request. It will display "error": "Invalid characters in URI or invalid URI format."



Everything after a question mark in a URL are search parameters, and must follow the format "parameter=value". The URL "https://nook.testrail.com/index.php?/api/v2/get_runs/45" is therefore invalid. You should discuss this with whoever created that API.