Put API Request in Python returning error


I'm fairly new to using API's, so bear with me here.

Using Postman, I'm able to make a Put request using JSON and it works fine. When I try to use the same JSON body in Python, I am getting this error:

{'code': 'E.Internal', 'error': 'An internal error has occurred processing your request. Please notify Customer Support for assistance.', 'status': 'error'}

The company's customer support is not too helpful so I wanted to see if someone here could help me instead.

Here is my script:

url = 'https://website.com/rest/site/' + record_id

json_body = (JSON body here, same one that works in Postman)
head = {'Accept':'application/json', 'Content-Type': 'application/json'}
response = requests.put(url, auth=(username, password), json=json_body, headers=head)
data = response.json()

Open in new window

If I change the requests.put to requests.get and drop everything after "auth=(username, password)" it works fine and returns the json of the record. What exactly am I doing wrong and how do I put in the data?
Emmett McKennaAsked:
Who is Participating?
Emmett McKennaAuthor Commented:
I figured out what my problem was.

This line

response = requests.put(url, auth=(username, password), json=json_body, headers=head)

Open in new window

should have been:

response = requests.put(url, auth=(username, password), data=json_body, headers=head)

Open in new window

For some reason I thought it needed to be "json=" instead of "data=".
Flabio GatesCommented:
For problems such as these, I inspect my web requests using Fiddler.
Capture the requests from Postman and from the python requests module then compare.
I find, from experience, that it is usually a missing/wrong header or that the request body (format) was wrong.
Emmett McKennaAuthor Commented:
This is what fixed it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.