Skip to main content
15-Moonstone
July 2, 2018
Question

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

  • July 2, 2018
  • 9 replies
  • 5043 views

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

22-Sapphire I
July 2, 2018

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

Thanks!

sbt15-MoonstoneAuthor
15-Moonstone
July 3, 2018

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

sbt15-MoonstoneAuthor
15-Moonstone
July 3, 2018

Hello PaiChung,

Is my script wrong??

 

Thanks,

Sunay

16-Pearl
July 16, 2018

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