Emac
Emac

Reputation: 1167

Put API Request in Python returning error

I'm fairly new to using API's, so bear with me here. I have searched for other problems like this, but haven't encountered any solutions for one that'll help my problem.

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()
print(data)

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, so I am able to connect to the API, just not put anything into it with Python. Again, basically the same exact thing works in Postman.

What exactly am I doing wrong and how do I put in the data?

Upvotes: 0

Views: 1317

Answers (1)

sehafoc
sehafoc

Reputation: 876

According to the requests documentation you're not filling out the put function correctly. Try it like this?

import json
import requests

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

headers = {
    "Content-Type": "application/json",
    'Accept':'application/json'
}

payload = json.dumps({
    "data_goes_here": None
})

rv = requests.put(url, data=payload, headers=headers, verify=False)
if rv.status_code > 399:
    rv.raise_for_status()
print(json.loads(rv.text))

Upvotes: 1

Related Questions