Skip to main content
Aug 2, 2022 8:15 PM UTC
Solved

ThingWorx not returning POST JSON Swagger response

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:

svisveswaraiya_0-1659470036267.png

 

TW:

svisveswaraiya_1-1659470179796.png

 

wcuiAnswer
Aug 25, 2022 7:48 AM UTC

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);

 

5 replies

Aug 2, 2022 9:50 PM UTC

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?

Aug 12, 2022 9:27 AM UTC

Hi @VladimirRosu_116627 ,

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);

Aug 4, 2022 8:41 AM UTC

As VladimirRosu pointed out, missing necessary header might cause PostJson fail and return Header instead of Response.

Can you recheck the parameters?

Aug 12, 2022 9:47 AM UTC

Hi @wcui ,

the headers are application/json, when I pass that it returns the headers.

 

svisveswaraiya_0-1660297609536.png

 

wcuiAnswer
Aug 25, 2022 7:48 AM UTC

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);