1 # -*- coding: utf-8 -*-
7 This module implements the Requests API.
9 :copyright: (c) 2012 by Kenneth Reitz.
10 :license: Apache2, see LICENSE for more details.
13 from . import sessions
16 def request(method, url, **kwargs):
17 """Constructs and sends a :class:`Request <Request>`.
19 :param method: method for the new :class:`Request` object.
20 :param url: URL for the new :class:`Request` object.
21 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
22 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
23 :param json: (optional) json data to send in the body of the :class:`Request`.
24 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
25 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
26 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
27 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
28 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
29 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
31 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
32 :param timeout: (optional) How long to wait for the server to send data
33 before giving up, as a float, or a :ref:`(connect timeout, read
34 timeout) <timeouts>` tuple.
35 :type timeout: float or tuple
36 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
37 :type allow_redirects: bool
38 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
39 :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``.
40 :param stream: (optional) if ``False``, the response content will be immediately downloaded.
41 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
42 :return: :class:`Response <Response>` object
43 :rtype: requests.Response
48 >>> req = requests.request('GET', 'http://httpbin.org/get')
52 # By using the 'with' statement we are sure the session is closed, thus we
53 # avoid leaving sockets open which can trigger a ResourceWarning in some
54 # cases, and look like a memory leak in others.
55 with sessions.Session() as session:
56 return session.request(method=method, url=url, **kwargs)
59 def get(url, params=None, **kwargs):
60 """Sends a GET request.
62 :param url: URL for the new :class:`Request` object.
63 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
64 :param \*\*kwargs: Optional arguments that ``request`` takes.
65 :return: :class:`Response <Response>` object
66 :rtype: requests.Response
69 kwargs.setdefault('allow_redirects', True)
70 return request('get', url, params=params, **kwargs)
73 def options(url, **kwargs):
74 """Sends a OPTIONS request.
76 :param url: URL for the new :class:`Request` object.
77 :param \*\*kwargs: Optional arguments that ``request`` takes.
78 :return: :class:`Response <Response>` object
79 :rtype: requests.Response
82 kwargs.setdefault('allow_redirects', True)
83 return request('options', url, **kwargs)
86 def head(url, **kwargs):
87 """Sends a HEAD request.
89 :param url: URL for the new :class:`Request` object.
90 :param \*\*kwargs: Optional arguments that ``request`` takes.
91 :return: :class:`Response <Response>` object
92 :rtype: requests.Response
95 kwargs.setdefault('allow_redirects', False)
96 return request('head', url, **kwargs)
99 def post(url, data=None, json=None, **kwargs):
100 """Sends a POST request.
102 :param url: URL for the new :class:`Request` object.
103 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
104 :param json: (optional) json data to send in the body of the :class:`Request`.
105 :param \*\*kwargs: Optional arguments that ``request`` takes.
106 :return: :class:`Response <Response>` object
107 :rtype: requests.Response
110 return request('post', url, data=data, json=json, **kwargs)
113 def put(url, data=None, **kwargs):
114 """Sends a PUT request.
116 :param url: URL for the new :class:`Request` object.
117 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
118 :param \*\*kwargs: Optional arguments that ``request`` takes.
119 :return: :class:`Response <Response>` object
120 :rtype: requests.Response
123 return request('put', url, data=data, **kwargs)
126 def patch(url, data=None, **kwargs):
127 """Sends a PATCH request.
129 :param url: URL for the new :class:`Request` object.
130 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
131 :param \*\*kwargs: Optional arguments that ``request`` takes.
132 :return: :class:`Response <Response>` object
133 :rtype: requests.Response
136 return request('patch', url, data=data, **kwargs)
139 def delete(url, **kwargs):
140 """Sends a DELETE request.
142 :param url: URL for the new :class:`Request` object.
143 :param \*\*kwargs: Optional arguments that ``request`` takes.
144 :return: :class:`Response <Response>` object
145 :rtype: requests.Response
148 return request('delete', url, **kwargs)