9

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!

1
  • Would you be fine disclosing the url - that will help me a lot answering this quickly, Thanks Commented Nov 25, 2014 at 13:18

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

1 Comment

Saved me after 3 hours of racking my brains! Kudos
-1

Do you want to pretty print your JSON data? Try this:

data_json = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

See https://docs.python.org/2/library/json.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.