Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X
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);
Solved! Go to Solution.
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 : 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 : 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.
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.
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 : 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 : 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.