Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
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
Can you also show the snippet you've tried so far and how you set it up?
Thanks!
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
Hello PaiChung,
Is my script wrong??
Thanks,
Sunay
What errors do you see in the logs?
also try this as a post text and set service return to string
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
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
Hello PaiChung,
Any info that you want to add up on this..??
Thanks.
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