Reputation: 3003
So I'm trying to POST a simple stuff onto my page using Python.
r = requests.Session().post(
'http://mypage.com/add?token=%s&title=%s&opt=&opt=true&token=%s' % (token, title, token),
headers = headers,
proxies = proxy,
timeout = max_timeout,
)
The problem is: if title
is, for example Hello World!
it works perfectly, but if title
is Hello World! Visit www.google.com
it won't work.
What I've found so far is that title
string wont send if it contains punctuations, in this case www.google.com
has two .
so it wont post...
Is this normal in Python? I've also tried to use urllib.quote
urllib.urlencode
and more, but same result...
In addition, if you ask why I don't use data = mydata
inside post()
object is because, if you have noticed, I'm using the param token
and opt
twice, so if I make a data
object like this:
data = {
"token": token,
"title": "title",
"opt": "",
"opt": 'value',
"token": token
}
Obviously it wont work as it has duplicated key
values.
Upvotes: 1
Views: 8342
Reputation: 1121854
You are POST-ing an empty body, and only using query parameters. requests
can send duplicate keys just fine, both in request parameters and in a POST body. All you have to do is use a sequence of key-value tuples instead of a dictionary:
params = [
("token", token),
("title", "title"),
("opt", ""),
("opt", 'value'),
("token": token),
]
These will be encoded for you when used as query parameters or as application/x-www-form-urlencoded POST body.
To send these as a POST body, use the data
keyword argument:
requests.post('http://mypage.com/add', data=params,
headers = headers, proxies = proxy, timeout = max_timeout)
or use params
to send these as query parameters (in the URL):
requests.post('http://mypage.com/add', params=params,
headers = headers, proxies = proxy, timeout = max_timeout)
Upvotes: 3