request

发送请求

发送请求,以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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值