Skip to main content
1-Visitor
October 29, 2019
Solved

API call with InfoTable input results in "Unable To Parse JSON Request"

  • October 29, 2019
  • 3 replies
  • 3277 views

I have a python script that's calling a ThingWorx Service. I'm calling a different service elsewhere in the script and it's working, but I'm not sure why this one isn't. The only difference is that this service has an InfoTable input.

 

Here's the relevant code.

 

 

 

propNames = {
 "dataShape": {
 "fieldDefinitions": {
 "name": {
 "name": "name",
 "baseType": "STRING"
 },
 "description": {
 "name": "description",
 "baseType": "STRING"
 }
 }
 },
 "rows": [{"name": "Stat_DisplaySpeed1"}]
}


params = {
 "maxItems": 500,
 "propertyNames": propNames,
 "startDate": startDate.replace(tzinfo=timezone.utc).isoformat(),
 "endDate": endDate.replace(tzinfo=timezone.utc).isoformat(),
 "query": ""
}

headers = {
 "appKey": PROD_KEY,
 "Accept": "application/json",
 "Content-Type": "application/json"
}

r = requests.post(url, params=params, headers=headers)

 

 

 

(startDate and endDate are defined elsewhere in the code)

 

This results in a status code of 406 with the body "Unable To Parse JSON Request"

 

Am I somehow forming "propertyNames" incorrectly? I'm confused about what's going on here.

Best answer by mdszy

I get a different error "accessing data provider" when I send without any data.

 

But going the route of QueryNumberPropertyHistory is working.

 

3 replies

16-Pearl
October 30, 2019

Could you please share your TWX service code?

mdszy1-VisitorAuthor
1-Visitor
October 30, 2019

This is calling the built in QueryNamedPropertyHistory service.

16-Pearl
October 30, 2019

I have not done this from Python. So, I could be wrong. Probably you need to enclose propNames value in double quotes(beginning and end) and escape all other ones. 

"{\"dataShape\":{\"fieldDefinitions\":{\"name\":{\"name\..........."

 

mdszy1-VisitorAuthor
1-Visitor
October 30, 2019

So you're saying propNames actually needs to be converted to a JSON string and passed along? Tried that, didn't work.

16-Pearl
October 30, 2019

Execute QueryNamedPropertyHistory from composer and use Chrome Developer tools to get the request payload being used for the execution. You could then modify this to generate your call. 

mdszy1-VisitorAuthor
1-Visitor
October 30, 2019

I have copy-pasted from Chrome's inspector and I still get the error.

18-Opal
October 30, 2019

Hello @mdszy ,

 

I would troubleshoot it as follows:

 

1. Try to call it without any parameters at all (empty JSON)

2. If it works, start adding parameters one by one in this order: maxItems, startDate / endDate (by the way, I think you might need them in milliseconds-since-epoch here, not ISO strings), and finally propertyNames

3. Alternatively, since you're querying only one property anyway, I would recommend using QueryStringPropertyHistory (or its analogue for Number, etc.), which doesn't require INFOTABLE parameter, but only a String

 

/ Constantine