6.Requests【接口请求】requests模拟发送post请求

一、前言

上节课我们讲到了如何通过requests模块模拟发送get请求,我们这节课学习一下另一个常见的http请求方式:post请求。与get不同的是,post请求可以传请求体,而get没有请求体。下面我们对如何发送一个简单的post请求作讲解。

二、学习目标

1.简单的post请求

三、知识点

1.【简单的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)

  • 语法:

    requests.post(url, data=None, json=None, **kwargs)
    
  • 参数:

    url(必填参数):接口的请求地址。

    data(选填参数):接口的请求体,传入的参数可以是字典格式(推荐),也可以是字符串。

    json(选填参数):接口的请求体,传入的参数可以是字典格式(推荐),也可以是字符串。

    **kwargs(选填,不定长参数):代表还可以传其他参数,如headers,cookies等。

  • 返回值:

    响应对象

  • 示例:

    import requests
    
    res = requests.post('http://www.httpbin.org/post')
    print(res.text) #text是响应对象的属性,指响应体的文本内容
    
posted @ 2023-01-17 10:08  测开星辰  阅读(131)  评论(0编辑  收藏  举报