Skip to main content
1-Visitor
January 10, 2019
Solved

Rest API returning HTML type response

  • January 10, 2019
  • 1 reply
  • 23760 views

 

Hi,

I have followed  https://community.ptc.com/t5/ThingWorx-Developers/rest-api-returns-an-html-page-How-to-access-the-data/m-p/501682

But still it seems there is some problem.

 

 I am using this piece of code from a node application, to get a property value using REST.

 

var data = JSON.stringify(false);
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
 if (this.readyState === 4) {
 console.log(this.responseText);
 }
});

xhr.open("GET", "http://localhost:8080/Thingworx/Things/ThingName_Example/Properties/uploadPath");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "text/xml");
xhr.setRequestHeader("appKey", "PPPP9f16-XXXX-XXXX-XXXX-1b719e2UUUUU");

xhr.send(data);
 
 
 
and the result i get is :- 
<HTML><HEAD><TITLE>Property Value For CMU_TH_DOCUMENT_REPO &#x3a; uploadPath</TITLE><LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK><META http-equiv='Content-Type' content='text/html'></META><META http-equiv='cache-control' content='no-cache, no-store'></META><META http-equiv='expires' content='-1'></META><META http-equiv='pragma' content='no-cache, no-store'></META></HEAD><BODY><IMG src="/Thingworx/images/ThingworxLogo.png"/><BR/><H1>Property Value For CMU_TH_DOCUMENT_REPO &#x3a; uploadPath</H1><TABLE><TR><TH>uploadPath</TH></TR><TR><TD>I am upload path</TD></TR></TABLE></BODY></HTML>
 
 
I think I have used correct header type, as suggested in one of the post, But I still get HTML type of response.
Is there anything I am missing?
Best answer by MarekP.Nowaczyk

I tested this simple example of node.js code:

var http = require("http");

var options = {
host: "localhost",
port: "8080",
path: "/Thingworx/Things/ExampleThing/Properties/name",
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "text/xml",
"appKey": "app_key"
}
};

http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();

 

That returned expected response in the body just like in Postman:

BODY: <?xml version="1.0" encoding="UTF-8" standalone="no"?><InfoTable><DataShape><FieldDefinitions><FieldDefinition aspect.isBuiltIn="true" aspect.isPersistent="false" aspect.isReadOnly="true" baseType="STRING" category="Metadata" description="Thing name" isLocalOnly="true" name="name" ordinal="0"/></FieldDefinitions></DataShape><Rows><Row><name><![CDATA[LocalThing]]></name></Row></Rows></InfoTable>

Or set "Accept" to "application/json" to get output in JSON format:

BODY: {"dataShape":{"fieldDefinitions":{"name":{"name":"name","description":"Thing name","baseType":"STRING","ordinal":0,"aspects":{"isReadOnly":true,"isPersistent":false,"isBuiltIn":true}}}},"rows":[{"name":"LocalThing"}]}

But when I set "Accept to "text/html" I get HTML body:

BODY: <HTML><HEAD><TITLE>Property Value For LocalThing &#x3a; name</TITLE><LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK><META http-equiv='Content-Type' content='text/html'></META><META http-equiv='cache-control' content='no-cache, no-store'></META><META http-equiv='expires' content='-1'></META><META http-equiv='pragma' content='no-cache, no-store'></META></HEAD><BODY><IMG src="/Thingworx/images/ThingworxLogo.png"/><BR/><H1>Property Value For LocalThing &#x3a; name</H1><TABLE><TR><TH>name</TH></TR><TR><TD>LocalThing</TD></TR></TABLE></BODY></HTML>

Try this and let me know if you get the expected response.

 

1 reply

15-Moonstone
January 10, 2019

Can you use Postman app to test the REST request and its parameters to make sure it works as expected and then use same in your service?

manasvi1-VisitorAuthor
1-Visitor
January 11, 2019

I have checked it through postman as well.

Attached is the postman response, which is is fine.

 

 

15-Moonstone
January 15, 2019

I tested this simple example of node.js code:

var http = require("http");

var options = {
host: "localhost",
port: "8080",
path: "/Thingworx/Things/ExampleThing/Properties/name",
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "text/xml",
"appKey": "app_key"
}
};

http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();

 

That returned expected response in the body just like in Postman:

BODY: <?xml version="1.0" encoding="UTF-8" standalone="no"?><InfoTable><DataShape><FieldDefinitions><FieldDefinition aspect.isBuiltIn="true" aspect.isPersistent="false" aspect.isReadOnly="true" baseType="STRING" category="Metadata" description="Thing name" isLocalOnly="true" name="name" ordinal="0"/></FieldDefinitions></DataShape><Rows><Row><name><![CDATA[LocalThing]]></name></Row></Rows></InfoTable>

Or set "Accept" to "application/json" to get output in JSON format:

BODY: {"dataShape":{"fieldDefinitions":{"name":{"name":"name","description":"Thing name","baseType":"STRING","ordinal":0,"aspects":{"isReadOnly":true,"isPersistent":false,"isBuiltIn":true}}}},"rows":[{"name":"LocalThing"}]}

But when I set "Accept to "text/html" I get HTML body:

BODY: <HTML><HEAD><TITLE>Property Value For LocalThing &#x3a; name</TITLE><LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK><META http-equiv='Content-Type' content='text/html'></META><META http-equiv='cache-control' content='no-cache, no-store'></META><META http-equiv='expires' content='-1'></META><META http-equiv='pragma' content='no-cache, no-store'></META></HEAD><BODY><IMG src="/Thingworx/images/ThingworxLogo.png"/><BR/><H1>Property Value For LocalThing &#x3a; name</H1><TABLE><TR><TH>name</TH></TR><TR><TD>LocalThing</TD></TR></TABLE></BODY></HTML>

Try this and let me know if you get the expected response.