Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X
Hi,
I am doing a postJson method in ThingWorx. But it is not returning the response body. When I execute the method in swagger, it is returning the response body but TW is returning just the headers. It is not returning like swagger but I wanted to read the response exactly like Swagger. How can we achieve this use case?
Swagger:
TW:
Solved! Go to Solution.
Below is sample code I tested PostJson on TW9.1.11.
I defined a thing “PostJsonTest” with a service "s1" which has a string parameter "p1".
As you can see, I manually set 5 parameters: header/url/content/username/password. Note that parameter "p1" and its value is put inside content.
Can you refer to this sample then change and test your code accordingly ?
---------------------------
let header = {
"Content-Type" : "application/json"
};
let content = {
"p1" : "aaa"
};
let params = {
proxyScheme: undefined /* STRING */,
headers: header /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://xxx/Thingworx/Things/PostJsonTest/Services/s1" /* STRING */,
content: content /* JSON */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: "xxx" /* STRING */,
domain: undefined /* STRING */,
username: "xxx" /* STRING */
};
// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);
What you see in the output is not the response headers, but the request header.
1. Which ThingWorx version is exhibiting this?
2. Are you sure you're replicating the same POST request in ThingWorx? I see no password/username in the screenshot you supplied, but maybe you're passing an authorization token in the header? What about the request body, is it the same as the one you supplied via Swagger?
Hi @VladimirRosu ,
1, The TW version is ThingWorx 9.1.0-b10877
2. Yes, I am replicating the same post request as Swagger but even if I pass the password and username it throws same error. It doesnt have any validation or authorization for the header.
In swagger, it works fine for the same content but in TW, it doesnt work.
I passed header as :
headers: {
'content-type: application/json'
}
content = {
"plant": "string",
"transType": "string",
"user": "string",
"productionOrderNumber": "string",
"serialNumber": "string",
"componentTrackingDetails": [
{
"component": "string",
"serialNumber": "string"
}
]
}
let params = {
proxyScheme: undefined /* STRING */,
headers: headers/* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: url /* STRING */,
content: content /* JSON */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};
var result = Resources["ContentLoaderFunctions"].PostJSON(params);
As VladimirRosu pointed out, missing necessary header might cause PostJson fail and return Header instead of Response.
Can you recheck the parameters?
Below is sample code I tested PostJson on TW9.1.11.
I defined a thing “PostJsonTest” with a service "s1" which has a string parameter "p1".
As you can see, I manually set 5 parameters: header/url/content/username/password. Note that parameter "p1" and its value is put inside content.
Can you refer to this sample then change and test your code accordingly ?
---------------------------
let header = {
"Content-Type" : "application/json"
};
let content = {
"p1" : "aaa"
};
let params = {
proxyScheme: undefined /* STRING */,
headers: header /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://xxx/Thingworx/Things/PostJsonTest/Services/s1" /* STRING */,
content: content /* JSON */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: "xxx" /* STRING */,
domain: undefined /* STRING */,
username: "xxx" /* STRING */
};
// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);