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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

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

YD_9850038
9-Granite

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

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

13 REPLIES 13

@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

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

@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

@PEHOWE 

 

Still showing the same error even after correcting the format as you said!

 

Regards, 

Yous

 

 

@YD_9850038 ,

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

mgoel
17-Peridot
(To:YD_9850038)

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, 

 

@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

 

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
 

@YD_9850038 @mgoel ,

 

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:

"aaa-org-ids" : [
        "ed8eb542-5a5e-46b0-80bd-a1768cc43da8"
    ],
and
"aaa-org-ids" : 
        "ed8eb542-5a5e-46b0-80bd-a1768cc43da8",
Have you tried both syntaxs.
 

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

@YD_9850038 , 

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 

@PEHOWE 

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?

@YD_9850038 ,

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:

PEHOWE_0-1622134390171.png

There is no body parameter which is accepted by the function.

Top Tags