简体   繁体   中英

python requests.post data without value don't work

how can i post only key of form data to a server like the site itself?

http_post_text_or_only_key_of_dict

In [1]: from requests import Request

In [2]: req = Request('POST', 'http://www.google.com', data={'json':''}).prepare()

In [3]: req.headers, req.body
Out[3]:
({'Content-Length': '5', 'Content-Type': 'application/x-www-form-urlencoded'},
 'json=')

In [4]: req = Request('POST', 'http://www.google.com', data={'json':None}).prepare()

In [5]: req.headers, req.body
Out[5]: ({'Content-Type': 'application/x-www-form-urlencoded'}, '')

the 'Content-Length' can't equal length of key, like 4. and, how can i make the body equal 'json' ? thx all

From the documentation :

There are times that you may want to send data that is not form-encoded. If you pass in a string instead of a dict , that data will be posted directly.

In your case, that means doing this:

req = Request('POST', 'http://www.google.com', data='json').prepare()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM