cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Rest API returning HTML type response

manasvi
12-Amethyst

Rest API returning HTML type response

 

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?
1 ACCEPTED SOLUTION

Accepted Solutions
MarekP.Nowaczyk
14-Alexandrite
(To:manasvi)

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.

 

View solution in original post

4 REPLIES 4
MarekP.Nowaczyk
14-Alexandrite
(To:manasvi)

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?

I have checked it through postman as well.

Attached is the postman response, which is is fine.

 

 

MarekP.Nowaczyk
14-Alexandrite
(To:manasvi)

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.

 

Thanks ! This works fine.

Top Tags