python+pytest接口自动化(5)-requests发送post请求

【文章末尾给大家留下了大量的福利】

简介

在HTTP协议中,与get请求把请求参数直接放在url中不同,post请求的请求数据需通过消息主体(request body)中传递。

且协议中并没有规定post请求的请求数据必须使用什么样的编码方式,所以其请求数据可以有不同的编码方式,服务端通过请求头中的 Content-Type 字段来获知请求中的消息主体是何种编码方式,再以对应方式对消息主体进行解析。

post请求参数常用的编码方式如下:

application/x-www-form-urlencoded		                # form表单格式,非常常见
multipart/form-data						# 一般用于上传文件,较为常见
application/json						# json字符串格式,非常常见
text/xml							# xml格式

关于post请求参数,后面会有文章专门讲述,这里不做过多的阐述。

requests.post()参数说明

使用requests库提供的post方法发送post请求,requests.post() 源码如下:

def post(url, data=None, json=None, **kwargs):
    r"""Sends a POST request.

    :param url: URL for the new :class:`Request` object.
    :param data: (optional) Dictionary, list of tuples, 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 \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    return request('post', url, data=data, json=json, **kwargs)

参数说明:

  1. url,请求网址

  2. data,字典、元组列表、字节或要发送到指定URL的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值