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 ` object
    :rtype: requests.Response
    """

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

参数说明:

  1. url,请求网址

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

你可能感兴趣的:(pytest,json,python,开发语言)