Formatting a nested json for use with Python Requests
The Requests library has a nasty habit of clobbering data when passing in nested JSON to the data
param. To avoid this, pass it into the json
param instead:
r = requests.put(PUT_URL, json=data_json, headers=headers, auth=(USER_NAME, USER_PASS), timeout=10)
For more details, take a look at this answer to a similar question: Post JSON using Python Requests
Admin
Updated on June 11, 2022Comments
-
Admin almost 2 years
I have been working on solving an HTTP 500 (bad syntax/string) error for far too long, and after doing some searching I cannot find a solution anywhere. I have a nested json PUT request that I have been able to make work using a couple API tools (both browser extensions and stand-alone programs), but when I try to use the json in Python's HTTP Requests module, I keep getting the 500 error code returned.
I have gotten other, simplier jsons (e.g. data={"RequestID": "71865"}) to work using similar code to the following, which leaves me to believe something is not getting formatted correctly, and I am unfortunately too new to this json-python thing to figure it out. I think the issue is because of the way python handles the nested json.
# -*- coding: utf-8 -*- #!/usr/bin/env python import requests import json USER_NAME=u"myusername" USER_PASS=u"mypassword" PUT_URL="https://webservice.url.com/A/Path/To/Create/" headers = {"Content-Type": "application/json"} data = { "ListOfFields": { "Field": [ {"fieldname": "summary","value": "test summary"}, {"fieldname": "notes","value": "an example json PUT"}, {"fieldname": "user","value": "myuser"} ] } } data_json = json.dumps(data) payload = {'json_playload': data_json } ## I have tried with and without this line. r = requests.put('{}'.format(PUT_URL), data=data_json, headers=headers, auth=(USER_NAME, USER_PASS), timeout=10) # r = requests.put('{}'.format(PUT_URL), data=payload, headers=headers, auth=(USER_NAME, USER_PASS), timeout=10)
I have tried putting the data value into quotes, a single line, and making some other slight tweaks, but I keep getting the 500 error.
print(r.status_code) >> 500
As mentioned before, I have gotten similar code to work in python using GET and POST and the same web server, but this one is giving me a headache!