Main Interface¶
All of Request’s functionality can be accessed by these 7 methods. They all return an instance of the Response object.
- requests.request(method, url, **kwargs)¶
Constructs and sends a Request. Returns Response object.
Parameters: - method – method for the new Request object.
- url – URL for the new Request object.
- params – (optional) Dictionary or bytes to be sent in the query string for the Request.
- data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
- headers – (optional) Dictionary of HTTP Headers to send with the Request.
- cookies – (optional) Dict or CookieJar object to send with the Request.
- files – (optional) Dictionary of ‘name’: file-like-objects (or {‘name’: (‘filename’, fileobj)}) for multipart encoding upload.
- auth – (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
- timeout – (optional) Float describing the timeout of the request.
- allow_redirects – (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
- proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
- verify – (optional) if True, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
- stream – (optional) if False, the response content will be immediately downloaded.
- cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
Usage:
>>> import requests >>> req = requests.request('GET', 'http://httpbin.org/get') <Response [200]>
- class requests.Response¶
The Response object, which contains a server’s response to an HTTP request.
- apparent_encoding¶
The apparent encoding, provided by the lovely Charade library (Thanks, Ian!).
- content¶
Content of the response, in bytes.
A CookieJar of Cookies the server sent back.
- elapsed¶
The amount of time elapsed between sending the request and the arrival of the response (as a timedelta)
- encoding¶
Encoding to decode with when accessing r.text.
- headers¶
Case-insensitive Dictionary of Response Headers. For example, headers['content-encoding'] will return the value of a 'Content-Encoding' response header.
- history¶
A list of Response objects from the history of the Request. Any redirect responses will end up here. The list is sorted from the oldest to the most recent request.
- iter_content(chunk_size=1, decode_unicode=False)¶
Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.
- iter_lines(chunk_size=512, decode_unicode=None)¶
Iterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses.
- json(**kwargs)¶
Returns the json-encoded content of a response, if any.
Parameters: **kwargs – Optional arguments that json.loads takes.
- links¶
Returns the parsed header links of the response, if any.
- raw¶
File-like object representation of response (for advanced usage). Requires that ``stream=True` on the request.
- status_code¶
Integer Code of responded HTTP Status.
- text¶
Content of the response, in unicode.
if Response.encoding is None and chardet module is available, encoding will be guessed.
- url¶
Final URL location of Response.
- requests.head(url, **kwargs)¶
Sends a HEAD request. Returns Response object.
Parameters: - url – URL for the new Request object.
- **kwargs – Optional arguments that request takes.
- requests.get(url, **kwargs)¶
Sends a GET request. Returns Response object.
Parameters: - url – URL for the new Request object.
- **kwargs – Optional arguments that request takes.
- requests.patch(url, data=None, **kwargs)¶
Sends a PATCH request. Returns Response object.
Parameters:
- requests.delete(url, **kwargs)¶
Sends a DELETE request. Returns Response object.
Parameters: - url – URL for the new Request object.
- **kwargs – Optional arguments that request takes.
Exceptions¶
- exception requests.RequestException¶
There was an ambiguous exception that occurred while handling your request.
- exception requests.ConnectionError¶
A Connection error occurred.
- exception requests.HTTPError(*args, **kwargs)¶
An HTTP error occurred.
- exception requests.URLRequired¶
A valid URL is required to make a request.
- exception requests.TooManyRedirects¶
Too many redirects.