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

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

sbt
Amethyst

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

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

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

Thanks!

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

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

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

Hello PaiChung,

Is my script wrong??

 

Thanks,

Sunay

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

What errors do you see in the logs?

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

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

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

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

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 

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

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

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

Hello PaiChung,

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

 

Thanks.

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

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

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________