2021-12-29http requests的几种参数

sample:resp_info = requests.post(list_url,headers=headers,json=data)

1.传递参数给url:https://httpbin.org/get?key2=value2&key1=value1

payload={'key1':'value1','key2':'value2'}

 r=requests.get('https://httpbin.org/get',params=payload)

2.提交一个form表单:

payload={'key1':'value1','key2':'value2'}

r=requests.post("https://httpbin.org/post",data=payload)

The data argument can also have multiple values for each key. This can be done by making data either a list of tuples or a dictionary with lists as values. This is particularly useful when the form has multiple elements that use the same key:

 payload_tuples=[('key1','value1'),('key1','value2')]>>> r1=requests.post('https://httpbin.org/post',data=payload_tuples)

 payload_dict={'key1':['value1','value2']}

 r2=requests.post('https://httpbin.org/post',data=payload_dict)

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.

For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data:

>>> importjson>>> url='https://api.github.com/some/endpoint'>>> payload={'some':'data'}>>> r=requests.post(url,data=json.dumps(payload))

3.json:Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

 url='https://api.github.com/some/endpoint'>>> payload={'some':'data'}>>> r=requests.post(url,json=payload)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容