cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
dk
dk
Level II

HTTP request returns html instead of json

hello

I tried to request JSON from an API using the following script:

request_neu = New HTTP Request(
	URL( "https://redacted.com/api/path?fileType=Json" ),
	Method( "GET" ),
	//headers( {"Accept: text/plain", "Accept: text/html", "Accept: */*"} ),
	//headers( {"Accept: */*"} ),
	//I tried everything here
	Headers( {"Accept: application/json"} ),
	Username( "api.user" ),
	Password( "redacted" ),
	Timeout( 900000 )
);

response = request_neu << send;
show(request_neu << get mime type);
show(request_neu << get status);
show(request_neu << get status message);
show(substr(response,1,999));
JSON to data table(response);

I added the debugging statements after the JSON to data table function resulted in an error and the output is

request_neu << get mime type = "text/html; charset=utf-8";
request_neu << get status = 200;
request_neu << get status message = "HTTP/1.1 200 OK";
Substr(response, 1, 999) = "
<!DOCTYPE html>
<html class=\!"no-js smooth-scrolling-html\!" lang=\!"en-GB\!" dir=\!"ltr\!">
<head>
etc

so essentially instead of JSON I receive html.

The interesting thing is that it works with Postman, Python, even Excel.

The really interesting thing is that some months ago when I created this script it also was working.

Do you have some idea what is going on here?

thx

2 REPLIES 2
jthi
Super User

Re: HTTP request returns html instead of json

Have you tried to see what the HTML contains? You could for example save it as .html page using Save Text File()

 

I don't quickly see anything wrong in your syntax, below is slightly modified example from scripting index

 

Names Default To Here(1);

request = New HTTP Request(
	url("http://httpbin.org/get"),
	Method("GET"),
	Headers({"Accept: application/json"}),
	Username("ross"),
	Password("Abc123"), 

);
data = request << Send;
Show(request << get response headers, request << get last url);

 

 

-Jarmo
dk
dk
Level II

Re: HTTP request returns html instead of json

When I looked at the html, it looks similar to the platform's web page except that the relative links are broken.

I am afraid that I will need to discuss with the people responsible for the platform since the mistake seems to originate from there.