jsonencodingwiql

How do You Encode WIQL JSON?


In Postman I'm able to submit a query to our Azure DevOps 2019 Server no problem:

POST https://<AZDOSERVER>/<COLLECTION>/<PROJECT>/<TEAM>/_apis/wit/wiql?api-version=5.0
{"query": "Select [System.Id] From WorkItems WHERE [System.AreaPath] UNDER '<AREANAME>'"}

But when I do it in Python it seems to run into an encoding issue with the single quotes around my AREANAME. Here's my code:

url = "https://<AZDOSERVER>/<COLLECTION>/<PROJECT>/<TEAM>/_apis/wit/wiql?api-version=5.0"
json = '{"query": "Select [System.Id] From WorkItems WHERE [System.AreaPath] UNDER ' 
    + "'<AREANAME>'" + '" }'
headers = {'Accept': 'application/json; api-version=5.0'}
response = request.post(url, json=json, auth=self.basicauth, headers=headers)

I get a 400 error with the following message:

b'{"count":1,"value":{"Message":"Error converting value \\"{\\"query\\": 
\\"Select [System.Id] From WorkItems WHERE [System.AreaPath] UNDER \'<AREANAME>\'\\" }\\" 
to type \'Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.Wiql\'. Path \'\', 
line 1, position 92.\\r\\n"}}'

How should I pass JSON properly into the python request.post() method so that it's sent wtih proper encoding? I tried capturing my outbound request via Fiddler but it doesn't see python traffic. I'm installing Wireshark too but that's going to take a while. I also stepped thru the request.post method to try and understand how it's build the body of the request. It seems to be handling the single quotes properly when it covers to a byte array.


Solution

  • Figured it out: request.post() takes a dictionary object for the json parameter instead of a json string.