Skip to main content
1-Visitor
May 26, 2021
Solved

External API request fails with error: Fail to parse request json

  • May 26, 2021
  • 2 replies
  • 3879 views

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,

 

Best answer by YD_9850038

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

2 replies

17-Peridot
May 26, 2021

@YD_9850038 ,

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

1-Visitor
May 26, 2021

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:

YD_9850038_0-1622042872155.png

 

 

Regards, 

Youssef

17-Peridot
May 26, 2021

@YD_9850038 ,

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

5-Regular Member
May 27, 2021

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

1-Visitor
May 27, 2021

Hello Guys, 

 

@PEHOWE i had same error with your code.

@mgoel i had the same error:

YD_9850038_0-1622102279422.png

 

I have observed that when i delete "Content-Type":"application/json" from the json, it returns anther error: 

YD_9850038_1-1622102430701.png

 

Thanks, 

Yous

 

1-Visitor
May 27, 2021

Hello Guys, 

 

I reproduced the same error with postman changing the body json:

YD_9850038_0-1622103791540.png

in postman it works fine with following format:

{
    "aaa-org-ids" : [
        "ed8eb542-5a5e-46b0-80bd-a1768cc43da8"
    ],
    "start" : "2021-05-03",
    "end" : "2021-05-09",
    "active" : "false"  
}
Thingworx does not accept this json !!!!
So any idea why it does not work
 
Regards, 
Yous