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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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=".

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Emmett McKennaAuthor Commented:
This is what fixed it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.