API
This part of the documentation covers all the interfaces of Requests. For
parts where Requests depends on external libraries, we document the most
important right here and provide links to the canonical documentation.
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, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=False, proxies=None, hooks=None, return_response=True)
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 or bytes 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 ‘filename’: file-like-objects for multipart encoding upload.
- auth – (optional) AuthObject to enable Basic 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.
- return_response – (optional) If False, an un-sent Request object will returned.
|
-
requests.head(url, **kwargs)
Sends a HEAD request. Returns Response object.
-
requests.get(url, **kwargs)
Sends a GET request. Returns Response object.
-
requests.post(url, data='', **kwargs)
Sends a POST request. Returns Response object.
-
requests.put(url, data='', **kwargs)
Sends a PUT request. Returns Response object.
-
requests.patch(url, data='', **kwargs)
Sends a PATCH request. Returns Response object.
-
requests.delete(url, **kwargs)
Sends a DELETE request. Returns Response object.
-
class requests.Response
The core Response object. All
Request objects contain a
response attribute, which is an instance
of this class.
-
content
Content of the response, in bytes or unicode
(if available).
-
cookies = None
A dictionary of Cookies the server sent back.
-
error = None
Resulting HTTPError of request, if one occured.
Case-insensitive Dictionary of Response Headers.
For example, headers['content-encoding'] will return the
value of a 'Content-Encoding' response header.
-
history = None
A list of Response objects from
the history of the Request. Any redirect responses will end
up here.
-
iter_content(chunk_size=10240, decode_unicode=None)
Iterates over the response data. 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.
-
ok = None
True if no error occured.
-
raise_for_status()
Raises stored HTTPError or URLError, if one occured.
-
raw = None
File-like object representation of response (for advanced usage).
-
request = None
The Request that created the Response.
-
status_code = None
Integer Code of responded HTTP Status.
-
url = None
Final URL location of Response.
Async
Utilities
These functions are used internally, but may be useful outside of
Requests.
Cookies
-
requests.utils.dict_from_cookiejar(cj)
Returns a key/value dictionary from a CookieJar.
| Parameters: | cj – CookieJar object to extract cookies from. |
-
requests.utils.cookiejar_from_dict(cookie_dict)
Returns a CookieJar from a key/value dictionary.
| Parameters: | cookie_dict – Dict of key/values to insert into CookieJar. |
-
requests.utils.add_dict_to_cookiejar(cj, cookie_dict)
Returns a CookieJar from a key/value dictionary.
| Parameters: |
- cj – CookieJar to insert cookies into.
- cookie_dict – Dict of key/values to insert into CookieJar.
|
Encodings
-
requests.utils.get_encodings_from_content(content)
Returns encodings from given content string.
| Parameters: | content – bytestring to extract encodings from. |
Returns encodings from given HTTP Header Dict.
| Parameters: | headers – dictionary to extract encoding from. |
-
requests.utils.get_unicode_from_response(r)
Returns the requested content back in unicode.
| Parameters: | r – Reponse object to get unicode content from. |
Tried:
- charset from content-type
- every encodings from <meta ... charset=XXX>
- fall back and replace all unicode characters
-
requests.utils.decode_gzip(content)
Return gzip-decoded string.
| Parameters: | content – bytestring to gzip-decode. |
Internals
These items are an internal component to Requests, and should never be
seen by the end user (developer). This part of the API documentation
exists for those who are extending the functionality of Requests.
Exceptions
-
exception requests.HTTPError(url, code, msg, hdrs, fp)
Raised when HTTP error occurs, but also acts like non-error return
-
exception requests.RequestException
There was an ambiguous exception that occured while handling your
request.
-
exception requests.AuthenticationError
The authentication credentials provided were invalid.
-
exception requests.URLRequired
A valid URL is required to make a request.
-
exception requests.InvalidMethod
An inappropriate method was attempted.
-
exception requests.TooManyRedirects
Too many redirects.
Classes
-
class requests.Request(url=None, headers={}, files=None, method=None, data={}, params={}, auth=None, cookiejar=None, timeout=None, redirect=False, allow_redirects=False, proxies=None, hooks=None)
The Request object. It carries out all functionality of
Requests. Recommended interface is with the Requests functions.
-
allow_redirects = None
Set to True if full redirects are allowed (e.g. re-POST-ing of data at new Location)
-
auth = None
AuthObject to attach to Request.
-
cookiejar = None
CookieJar to attach to Request.
-
data = None
Dictionary or byte of request body data to attach to the
Request.
-
files = None
Dictionary of files to multipart upload ({filename: content}).
Dictonary of HTTP Headers to attach to the Request.
-
hooks = None
Event-handling hooks.
-
method = None
HTTP Method to use.
-
params = None
Dictionary or byte of querystring data to attach to the
Request.
-
redirect = None
True if Request is part of a redirect chain (disables history
and HTTPError storage).
-
response = None
Response instance, containing
content and metadata of HTTP Response, once sent.
-
send(anyway=False)
Sends the request. Returns True of successful, false if not.
If there was an HTTPError during transmission,
self.response.status_code will contain the HTTPError code.
Once a request is successfully sent, sent will equal True.
| Parameters: | anyway – If True, request will be sent, even if it has |
already been sent.
-
sent = None
True if Request has been sent.
-
timeout = None
Float describ the timeout of the request.
-
url = None
Request URL.