requests.api

2023-05-16

# -*- coding: utf-8 -*-

"""
requests.api
~~~~~~~~~~~~

This module implements the Requests API.

:copyright: (c) 2012 by Kenneth Reitz.
:license: Apache2, see LICENSE for more details.
"""

from . import sessions


def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the body of the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) A JSON serializable Python object 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.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use. Defaults to ``True``.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response

    Usage::

      >>> import requests
      >>> req = requests.request('GET', 'https://httpbin.org/get')
      <Response [200]>
    """

    # By using the 'with' statement we are sure the session is closed, thus we
    # avoid leaving sockets open which can trigger a ResourceWarning in some
    # cases, and look like a memory leak in others.
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)


def get(url, params=None, **kwargs):
    r"""Sends a GET request.

    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)


def options(url, **kwargs):
    r"""Sends an OPTIONS request.

    :param url: URL for the new :class:`Request` object.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    kwargs.setdefault('allow_redirects', True)
    return request('options', url, **kwargs)


def head(url, **kwargs):
    r"""Sends a HEAD request.

    :param url: URL for the new :class:`Request` object.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    kwargs.setdefault('allow_redirects', False)
    return request('head', url, **kwargs)


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)


def put(url, data=None, **kwargs):
    r"""Sends a PUT 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('put', url, data=data, **kwargs)


def patch(url, data=None, **kwargs):
    r"""Sends a PATCH 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('patch', url, data=data, **kwargs)


def delete(url, **kwargs):
    r"""Sends a DELETE request.

    :param url: URL for the new :class:`Request` object.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    return request('delete', url, **kwargs)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

requests.api 的相关文章

  • Rails 中的 Google freebusy api 调用无法识别参数

    我试图从我的主日历中查找所有空闲 忙碌时间 但我无法让查询识别我的参数 在我的控制器中我有 freetimes client execute api method gt service freebusy query parameters g
  • EPi服务器开发

    除了 Episerver com 之外 使用EPiServer开发的人还使用哪些其他网站作为开发资源 一直在使用 coderesort com 但我发现它缺乏如何做事的示例 非常感谢 J 我用于EPiServer开发的通用资源 EPi服务器
  • 生产者程序中的 kafka 网络处理器错误(ArrayIndexOutOfBoundsException:18)

    我有下面的 kafka Producer Api 程序 我对 kafka 本身是新手 下面的代码从 API 之一获取数据并将消息发送到 kafka 主题 package kafka Demo import java util Propert
  • SQLAPI++ 的免费替代品? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有任何免费 也许是开源 的替代品SQLAPI http www sqlapi com 这个库看起来
  • 歌曲搜索和流预览 API [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有 API 可以让我搜索歌曲 然后在我的网站上流式传输该歌曲的预览 查看了 Amazon MP3 但
  • 如何共享 Swagger 文档

    我最近开始使用 Swagger 来编写文档 但有一些事情我仍然不清楚 我创建了 YAML 文档 现在我希望能够与团队的其他成员共享 pdf 或 HTML Javascript 页面中的文档 我无法使用 SwaggerHub 因为它们没有私有
  • flutter中api调用哪种方式最好

    我是颤振开发的新手 最近听说了Dio和Http包用于api调用 这是 api 调用的最佳选择 如果有人有更好的api服务方式 CreateAccountRepository internal static final CreateAccou
  • 为 REST API 编写单元测试的最佳方法是什么?

    在为 API 包装器编写单元测试时 我应该对 REST API 端点进行真正的调用 还是应该使用 mocl 响应来模拟成功和错误的调用 单元测试意味着只测试你的unit API 包装器 仅此而已 因此 不幸的是 您应该模拟整个 API 另一
  • 如何使用授权 API 设置部分身份验证

    好的 我正在通过 Authorize net API 设置部分付款 以便能够使用多张卡支付单笔余额 费用 我假设他们的部分身份验证功能涵盖了我的用例 但在测试中 我可以在此处使用 API 实时控制台向您展示一个问题 https develo
  • API 调用时出现 UnicodeEncodeError (json)

    我正在尝试打印此 API 调用的结果 但收到 UnicodeEncodeError 可能是超级菜鸟问题 但非常感谢任何帮助 import http client import json api key hidden connection h
  • 为什么 Twitter API 返回错误的推文 ID?

    我使用 Twitter API 来检索用户主页时间线推文 我使用 json 响应格式 最近推文 ID 在 API 中只是 id 被重新调整错误 举个例子 通常它应该像这样返回 id 14057503720 示例来自twitter控制台 但是
  • 使用启用了两步身份验证的 python 脚本从 nextcloud 下载文件

    我设置了一个 nextcloud 实例 我想使用 python 脚本从那里下载文件 我的 nextcloud 实例对所有用户强制执行两步身份验证 我希望它保持这种状态 我梦想的场景是使用requests库 因此请按照此处的文档进行操作htt
  • Flask Restful API url

    我正在使用 Flask RESTful http flask restful readthedocs org en latest index html http flask restful readthedocs org en latest
  • 在 Android KitKat 中接收彩信

    所以这个视频Android 4 4 短信 API http www youtube com watch v mdq0R2WQssQ DevBytes 解释了 KitKat 中 SMS API 的最新变化 他们还提供了示例项目的链接 http
  • C++ API 设计和错误处理

    我需要使用 lib 文件 MSVC 编写 C API 它由 Dll 公开的几个导出的 C 类组成 从我的另一个问题的答案中我了解到 如果 C API 是在一个 VC 版本 假设是 2010 中构建的 并且客户端代码是在另一个 VC 版本中编
  • axios响应不显示数据

    我正在学习使用 Axios 但输出如图所示 当我使用 fetch 时 输出正常 我该如何修复 Axios const axios require axios default const url https jsonplaceholder t
  • Skype API 的实现[重复]

    这个问题在这里已经有答案了 可能的重复 C 中的 Skype 插件 https stackoverflow com questions 1149615 skype addon in c sharp 如何在 C 中实现 Skype API 来
  • 如何通过groovy动态更新ReadyAPI/SoapUI中的Resource值?

    我的资源采用这种格式 testing 101 getCustomer 99 这里我需要通过 groovy 动态更改 101 和 99 部分 以便我可以在同一测试用例中运行多个值 我研究了 ReadyAPI 的内置功能 但没有那么有帮助 我也
  • 我们如何获取R中的商品价格?

    正如标题 我知道我们可以使用quantmod包来获取股票价格 但我们如何检索黄金 石油或农产品等商品价格 Use Quandl包 这里有一些例子 Gold lt Quandl LBMA GOLD WTI lt Quandl CHRIS CM
  • 有没有 API 可以在两个 iphone/ipod Touch/ipad 之间共享数据? (GameKit 除外)[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有任何框架 API可以轻松找到其他设备并在它们之间共享数据 请不是游戏包 bonjour 会很棒

随机推荐