Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a nested json for use with Python Requests

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!

like image 779
Sean Roux Avatar asked Nov 25 '14 12:11

Sean Roux


People also ask

How does Python handle nested JSON?

Python has built in functions that easily imports JSON files as a Python dictionary or a Pandas dataframe. Use pd. read_json() to load simple JSONs and pd. json_normalize() to load nested JSONs.

How do you format a JSON in Python?

In Python, you can create JSON string by simply assigning a valid JSON string literal to a variable, or convert a Python Object to JSON string using json. loads() function.

How do you pass JSON data in a post request in Python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

How do I process nested JSON?

We can parse a nested JSON object using the getString(index) method of JSONArray. This is a convenience method for the getJSONString(index). getString() method and it returns a string value at the specified position.


1 Answers

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

like image 106
Venrix Avatar answered Nov 15 '22 20:11

Venrix