204
Views
16
Comments
html to json
Question

I am consuming a REST API, and it returns an HTML type response, how can I convert it to a json object

2020-02-28 09-46-54
Eduardo Jauch

Hi kaoutar,

Is this expected? If not, what is the expected result of the REST API you're consuming?
I would say that you are being redirected to a login page and the response is the login page? 

Cheers.

UserImage.jpg
kaoutar elkarkri

Eduardo Jauch wrote:

Hi kaoutar,

Is this expected? If not, what is the expected result of the REST API you're consuming?
I would say that you are being redirected to a login page and the response is the login page? 

Cheers.

no, the login is done correctly.
What happens is that, after doing the login, it goes to another screen, on this screen I have a TableRecord where the response(data) of a GET call is loaded.
The problem is: the result of the call (when I do the Test) is an html text, not a json object


2020-02-28 09-46-54
Eduardo Jauch

Sorry, Let's start again from the beginning.

You said you're consuming a REST API. How are you calling the API method? Why the method is returning HTML and not JSON? Is this REST API you're consuming yours or is a Third Part REST API?

Cheers.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Kaoutar,

The only "valid" reason that we see, in the "wild" that an HTML response is returned instead of a valid JSON response is when you call a wrong URL, and you get a response other than 200 (but one in the 400 range (like "404 resource not found") or 500 range (like "503 service unavailable")).

There is no way to "convert" HTML to JSON. HTML is not a valid REST method response, and should not be returned by any REST service when there's no error (i.e. the response is in the 200 range).

You have a different problem here, please investigate what is returned exactly (if you don't know how, see my post here).

UserImage.jpg
kaoutar elkarkri

Kilian Hekhuis wrote:

Hi Kaoutar,

The only "valid" reason that we see, in the "wild" that an HTML response is returned instead of a valid JSON response is when you call a wrong URL, and you get a response other than 200 (but one in the 400 range (like "404 resource not found") or 500 range (like "503 service unavailable")).

There is no way to "convert" HTML to JSON. HTML is not a valid REST method response, and should not be returned by any REST service when there's no error (i.e. the response is in the 200 range).

You have a different problem here, please investigate what is returned exactly (if you don't know how, see my post here).


thank you :)

UserImage.jpg
kaoutar elkarkri

maybe I have an error in the login. How can I login using api Rest post?

2020-02-28 09-46-54
Eduardo Jauch

Ah... Now we're talking the same language again :D

It depends... How the REST API expects the authentication?
For example, if it expects basic authentication, you can setup it both in the code as well as in the Service Center:
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Consume_a_REST_API
and
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Configure_a_Consumed_REST_API_at_Runtime
If it needs something different, you may need to use the REST OnBeforeRequest (for example), to manipulate the REST request to add things for authentication.

It will depend on the REST you are calling.


UserImage.jpg
kaoutar elkarkri

Eduardo Jauch wrote:

Ah... Now we're talking the same language again :D

It depends... How the REST API expects the authentication?
For example, if it expects basic authentication, you can setup it both in the code as well as in the Service Center:
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Consume_a_REST_API
and
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Configure_a_Consumed_REST_API_at_Runtime
If it needs something different, you may need to use the REST OnBeforeRequest (for example), to manipulate the REST request to add things for authentication.

It will depend on the REST you are calling.


I'll try again, thanks for your help :)


UserImage.jpg
kaoutar elkarkri

 HEEEEEEEEEEEEELP

I make a REST request (post) , using postman, the answer is as follows:


I make the same request by consuming a REST api (post) from outsystem and it returns the following:
can someone help me to know where the problem comes from and how to solve this plzzzz

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Kaoutar,

I would assume that's because in Postman, you have entered credentials, and in OutSystems, you have not - given that there's no userInfo. What's strange though is that both seem to reply with a 200 OK response.

2020-02-28 09-46-54
Eduardo Jauch

Kilian Hekhuis wrote:

Hi Kaoutar,

I would assume that's because in Postman, you have entered credentials, and in OutSystems, you have not - given that there's no userInfo. What's strange though is that both seem to reply with a 200 OK response.

This is probably because they are not using the returning code "semantically". Login failing or not, they seem to be returning 200.

UserImage.jpg
kaoutar elkarkri

I have entered the same credentials in both, I made the same request, but the result is different

2020-02-28 09-46-54
Eduardo Jauch

kaoutar elkarkri wrote:

I have entered the same credentials in both, I made the same request, but the result is different

Did you check if you are using the same method as in Postman (POST, GET, etc)?
How are you passing the credentials in Postman and in OutSystems (Basic Authentication)?
Are you really sure you are passing the same credentials in both sides? Like, no typos, etc?

Cheers.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Still, what is sent is different, or you wouldn't get a different response :).

UserImage.jpg
kaoutar elkarkri

request Postman:





Request Outsystems:


in case of putting the wrong credentials, it returns an error, not "200 ok"

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

That's not the right error code either :). Anyway, the "test" REST is difficult to debug. Is advise you to makes a testing screen, and set the REST logging to full, to see what is actually sent and received.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.