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

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

mdszy
12-Amethyst

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

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.

ACCEPTED SOLUTION

Accepted Solutions
mdszy
12-Amethyst
(To:Constantine)

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

 

But going the route of QueryNumberPropertyHistory is working.

 

View solution in original post

8 REPLIES 8

Could you please share your TWX service code?

mdszy
12-Amethyst
(To:rjanardan)

This is calling the built in QueryNamedPropertyHistory service.

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\..........."

 

mdszy
12-Amethyst
(To:rjanardan)

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

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. 

mdszy
12-Amethyst
(To:rjanardan)

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

Constantine
17-Peridot
(To:mdszy)

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

mdszy
12-Amethyst
(To:Constantine)

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

 

But going the route of QueryNumberPropertyHistory is working.

 

Announcements


Top Tags