Reputation: 67
I'm trying to do a form login on a page and I keep getting the below type error. I have read the Python Requests package documentation and when I print my data dictionary it looks like a valid example. I'm not sure what is going wrong. Here is my code with Traceback:
import requests
accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
acceptlang = "en-US,en;q=0.9"
url = 'https://httpbin.org/post'
userid = 'username'
passwd = 'password'
headers = {
'Accept': accept,
'Accept-Language': acceptlang,
}
data = {userid: fakeuserid, passwd: fakepasswd}
>>> print(data)
{'username': '[email protected]', 'password': '0#CCJyy3^5Tu(Z'}
>>> response = requests.post(url, headers, data=data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: post() got multiple values for argument 'data'
When I POST with either only (url, headers) or (url, data=data) the post succeeds. I'm not sure what is going on here.
Upvotes: 2
Views: 1208
Reputation: 6371
Some API servers only accept JSON-Encoded POST/PATCH data, do like this:
response = requests.post(url, json=data, headers=headers)
It just the same as:
import json
response = requests.post(url, json.dumps(data), headers=headers)
See more at: docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
Upvotes: 0
Reputation: 897
According to the requests API, it looks as if you need the keyword for your headers
argument, without which post() assumes it's data
. Try this:
response = requests.post(url, headers=headers, data=data)
Upvotes: 1