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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

How to call an external rest api which accepts header and body as input.

sbt
13-Aquamarine
13-Aquamarine

How to call an external rest api which accepts header and body as input.

Hello,

I would like to achieve the same call through thingworx what i am able to do it through postman(attachment for reference) . 

 

I tried using snippets but was not successful.

I have to pass 2 header information and 4 body information with the url to get the JSON as a reply. How to send it through thingworx ??

 

Thanks

9 REPLIES 9
PaiChung
22-Sapphire I
(To:sbt)

Can you also show the snippet you've tried so far and how you set it up?

Thanks!

sbt
13-Aquamarine
13-Aquamarine
(To:PaiChung)

Hello PaiChung,

Below is the snippet i have used. I have passed the body in content.

//JSON object to send
var HeaderJson = {
"Content-Type" : "application/json",
"Accept" : "application/json"
}
//JSON object to send
var BodyJson = {
"grant_type" : "",
"resource" : "",
"client_id" : "",
"client_secret" : ""
}
var params = {
headers: HeaderJson /* JSON */,
url: "" /* STRING */,
content: BodyJson /* JSON */
};

// result: JSON
var jsonresult = Resources["ContentLoaderFunctions"].PostJSON(params);

 

Thanks,

Sunay

sbt
13-Aquamarine
13-Aquamarine
(To:sbt)

Hello PaiChung,

Is my script wrong??

 

Thanks,

Sunay

PaiChung
22-Sapphire I
(To:sbt)

What errors do you see in the logs?

also try this as a post text and set service return to string

sbt
13-Aquamarine
13-Aquamarine
(To:PaiChung)

Hello Pichung,
Yes I will try the option. But I would like to know is it correct to pass the body json to the content parameter ??

Thanks
sbt
13-Aquamarine
13-Aquamarine
(To:sbt)

Hello PaiChung,

I tried with both the options 

//JSON object to send
var HeaderJson = {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
}
//JSON object to send 
var BodyJson = {
'grant_type' : 'client_credentials',
'resource' : ' provided resource'
'Trace ID' : 'provided proper trace id',
'client_secret' : ' provide client secret'
}


var params = {
headers: HeaderJson /* JSON */,
url: "https://comlete url" /* STRING */,
content: BodyJson /* JSON */
};
// result: STRING
var result = Resources["ContentLoaderFunctions"].PostText(params);

logger.warn("testing Response:"+result);

PostText output message: {"error":"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: \r\nCorrelation ID: \r\nTimestamp: 2018-07-04 14:16:51Z","error_codes":[90014],"timestamp":"2018-07-04 14:16:51Z","trace_id":"","correlation_id":""}^Accept=application/json:Content-Type=application/json

Although the message says body should contain correlation id and others, i am able to get the data from postman without those parameters.

 

var jsonresult = Resources["ContentLoaderFunctions"].PostJSON(params);
logger.warn("testing Response:"+jsonresult.Result);

PostJSON output message: undefined.

 

Also i have tried sending the body embedded with the url to receive the token by encodeURI(body)

"https:complete url/token?encodeURI(grant_type=&resource=&client_id=&client_secret=)"

result is undefined.

 

am i missing something in my script.?

 

Thanks,

Sunay 

sbt
13-Aquamarine
13-Aquamarine
(To:PaiChung)

Hello PaiChung,

I tried with both the options

//JSON object to send
var HeaderJson = {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
}
//JSON object to send
var BodyJson = {
'grant_type' : 'client_credentials',
'resource' : ' provided resource'
'Trace ID' : 'provided proper trace id',
'client_secret' : ' provide client secret'
}


var params = {
headers: HeaderJson /* JSON */,
url: "https://comlete url" /* STRING */,
content: BodyJson /* JSON */
};
// result: STRING
var result = Resources["ContentLoaderFunctions"].PostText(params);

logger.warn("testing Response:"+result);

PostText output message: {"error":"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: \r\nCorrelation ID: \r\nTimestamp: 2018-07-04 14:16:51Z","error_codes":[90014],"timestamp":"2018-07-04 14:16:51Z","trace_id":"","correlation_id":""}^Accept=application/json:Content-Type=application/json

Although the message says body should contain correlation id and others, i am able to get the data from postman without those parameters.

 

var jsonresult = Resources["ContentLoaderFunctions"].PostJSON(params);
logger.warn("testing Response:"+jsonresult.Result);

PostJSON output message: undefined.

 

Also i have tried sending the body embedded with the url to receive the token by encodeURI(body)

"https:complete url/token?encodeURI(grant_type=&resource=&client_id=&client_secret=)"

result is undefined.

 

am i missing something in my script.?

 

Thanks,

Sunay

sbt
13-Aquamarine
13-Aquamarine
(To:PaiChung)

Hello PaiChung,

Any info that you want to add up on this..??

 

Thanks.

hchanana
16-Pearl
(To:sbt)

Hi @sbt,

 

Please try the following script :

 

//JSON object to send
var HeaderJson = {
"Content-Type" : "application/x-www-form-urlencoded",
"Accept" : "application/json"
}

var content = "grant_type=client_credentials&resource=http://some_url&client_id=xxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxx'"


var params = {
headers: HeaderJson /* JSON */,
url: "http://{some_url}" /* STRING */,
content: content
};

// result: JSON
var jsonresult = Resources["ContentLoaderFunctions"].PostText(params);

 

 

Thanks,

Himanshu

 

If my response has answered your question, please mark as Accepted Solution, for the benefit of others who may have the same question

Top Tags