Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Hello Experts,
I am using the PostJSON of ContentLoaderFunctions to retrieve some data form 3rd party application and i have this error as output:
"error-info": "Fail to parse request json",
"http-status": 400
below the code of my snippet:
let HmacHeaders = {
'Content-Type':'application/json',
'Accept':'*/*',
'Accept-Encoding':'gzip, deflate, br',
'X-HP-HMAC-Authentication': '****************************************************',
'X-HP-HMAC-Date': '2021-05-26T12:31:19.292Z',
'X-HP-HMAC-Algorithm': 'SHA1',
};
let Hmacurl="https://*************************/api/3d/v2/jobs/_search-by-orgs";
let Hmacbody={
"aaa-org-ids" :
"**********************************",
"start" : "2021-05-03",
"end" : "2021-05-09",
"active" : "false"
};
let params = {
headers: HmacHeaders /* JSON */,
url: Hmacurl /* STRING */,
body:Hmacbody
};
// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);
and as output i have while i am expecting data in JSON format:
{
"headers": {
"Accept": "*/*",
"X-HP-HMAC-Authentication": "***************************************************",
"X-HP-HMAC-Algorithm": "SHA1",
"Accept-Encoding": "gzip, deflate, br",
"X-HP-HMAC-Date": "2021-05-26T12:31:19.292Z",
"Content-Type": "application/json"
},
"error-info": "Fail to parse request json",
"http-status": 400
}
Thanks in advance,
Solved! Go to Solution.
Hello,
@PEHOWE i have tried both syntax and did not work however with the following syntax it worked fine:
let params = {
headers:{ "Content-Type":"application/json","Accept":"*/*","Accept-Encoding":"gzip, deflate, br","X-HP-HMAC-Authentication": "****************","X-HP-HMAC-Date": "2021-05-27T15:11:26.475Z","X-HP-HMAC-Algorithm": "SHA1"} /* JSON */,
url: "*****************************************************************" /* STRING */,
content: {"aaa-org-ids" : *******************,"start" : "2021-05-03","end" : "2021-05-09", "active" : "false"} /* JSON */,
};
// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);
I think that it is a bug in that version of thingworx, it should work with that format as well.
ThingWorx 9.1.1-b483 .
Thanks guys,
Yous
It looks like you are using single quotes in the JSON. I believe you need to use Double Quote.
Also for testing purpose to get all the parameter correct POSTMAN is a very helpful tool.
Peter
Hello,
I changed to double qoute but still receiving the same error.
I have validated the request with postmanbefore moving to implement it in thingworx service, so with postman it works fine:
Regards,
Youssef
I put your JSON into a web site which checks the format an it had an issue. I updated, changed the quotes and removed the last comment. The result did pass the JSON formatter.
{
"Content-Type":"application/json",
"Accept":"*/*",
"Accept-Encoding":"gzip, deflate, br",
"X-HP-HMAC-Authentication": "****************************************************",
"X-HP-HMAC-Date": "2021-05-26T12:31:19.292Z",
"X-HP-HMAC-Algorithm": "SHA1"
}
Please compare the my JSON with yours and let me know
Peter
Not sure why you are getting the same error? Correcting the JSON format should allow the parse. There may still be an error, but it should have changed. Please review this code:
HmacHeaders =
{"Content-Type":"application/json",
"Accept":"*/*",
"Accept-Encoding":"gzip, deflate, br",
"X-HP-HMAC-Authentication": "****************************************************",
"X-HP-HMAC-Date": "2021-05-26T12:31:19.292Z",
"X-HP-HMAC-Algorithm": "SHA1"
};
Hmacurl="https://*************************/api/3d/v2/jobs/_search-by-orgs";
Hmacbody={
"aaa-org-ids" :
"**********************************",
"start" : "2021-05-03",
"end" : "2021-05-09",
"active" : "false"
};
params = {
headers: HmacHeaders /* JSON */,
url: Hmacurl /* STRING */,
body:Hmacbody
};
// result: JSON
result = Resources["ContentLoaderFunctions"].PostJSON(params);
The above code returns the error: Error executing service TestJson. Message :: ************************* - See Script Error Log for more details.
Have you reviewed the ScriptErrorLog.log file for additional information?
What version of ThingWorx are you using?
Let me know what you find.
Peter
Hi @YD_9850038
Can you check below article if this is your requirement:
https://www.ptc.com/en/support/article/CS337595
Try using postText instead of PostJson in last line. I have seen few of use case where code was working fine in postman but not in ThingWorx with 400 error. Do try it and Let me know in case of any question.
Regards,
Mohit
Hello Guys,
I reproduced the same error with postman changing the body json:
in postman it works fine with following format:
Can you confirm what version of ThingWorx you are using? (Want to verify on the same version.)
In Composer if you go to Browse -> Resources -> ContentLoaderFunctions -> Services -> PostJSON
you can manually enter the parameters for the function and press the execute button. It should be the same values you are using in POSTMAN or your code. The interface might provide display any issue in a different manor.
In your post you show:
Peter
Hello,
@PEHOWE i have tried both syntax and did not work however with the following syntax it worked fine:
let params = {
headers:{ "Content-Type":"application/json","Accept":"*/*","Accept-Encoding":"gzip, deflate, br","X-HP-HMAC-Authentication": "****************","X-HP-HMAC-Date": "2021-05-27T15:11:26.475Z","X-HP-HMAC-Algorithm": "SHA1"} /* JSON */,
url: "*****************************************************************" /* STRING */,
content: {"aaa-org-ids" : *******************,"start" : "2021-05-03","end" : "2021-05-09", "active" : "false"} /* JSON */,
};
// result: JSON
let result = Resources["ContentLoaderFunctions"].PostJSON(params);
I think that it is a bug in that version of thingworx, it should work with that format as well.
ThingWorx 9.1.1-b483 .
Thanks guys,
Yous
Sounds like you found a solution. which is great.
It is kind of difficult to tell the difference between the format that worked and the format which did not work.
Did you change the JSON to be a single line?
If you did make it a single line, could you please add a space after each comma. That may affect how the JSON is parsed. I would like to identify the issue so that it can be addressed.
Thanks
Peter
the main root cause is body should be replaced by content independently if the json is one line or several.
if change body that i have in the code i posted in previous comments by content then it works fine!!!!
For me it should be Body and not content?
Thank you for responding. I think I understand what you changed
The original code you used:
params = {
headers: HmacHeaders /* JSON */,
url: Hmacurl /* STRING */,
body:Hmacbody
};
The code which worked:
params = {
headers: HmacHeaders /* JSON */,
url: Hmacurl /* STRING */,
content:Hmacbody
};
If you look at the parameters to the function PostJSON:
There is no body parameter which is accepted by the function.