发送请求
发送请求,以post为例
1.param形式传参:
import requests
url = 'http://www.wozuishuai.com'
param= {
"params1":"value1"
}
requests.post(url=url, params=param)
2.body形式传参:
url = 'http://www.wozuishuai.com'
body = {
"params1":"value1"
}
requests.post(url=url, json=body)
3.x-www-form-urlencoded传参格式:
import requests
url = 'http://www.wozuishuai.com'
data = {
"params1":"value1"
}
requests.post(url=url, data=data)
4.multipart/form-data传参格式:
import requests
url = 'http://www.wozuishuai.com'
files= [
('authorId', (None, eve.user_id)),
('subject', (None, '测试测试测试测试?')),
('bodyText', (None, '测试测试测试测试测试测试测试测试测试测试测试测试')),
('postType', (None, 'QA')),
('coins', (None, '20')),
('imgs',('1.png',open(your path)))
]
r = requests.post(url=url, files=files)
5.定制header
import requests
headers={
'Connection': 'keep-alive',
'Content-Type': 'multipart/form-data; '
}
url = 'http://www.wozuishuai.com'
r = requests.get(url=url,headers=headers)
聪明的人已经看出来了,想传什么参数只要对应一下参数名就OK了。其实requests里面有介绍的:
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary or list of tuples ``[(key, value)]`` (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.