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
. ReturnsResponse
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
. - json – (optional) json data 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 (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
- allow_redirects (bool) – (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]>
- method – method for the new
-
class
requests.
Response
¶ The
Response
object, which contains a server’s response to an HTTP request.-
apparent_encoding
¶ The apparent encoding, provided by the chardet library
-
close
()¶ Releases the connection back to the pool. Once this method has been called the underlying
raw
object must not be accessed again.Note: Should not normally need to be called explicitly.
-
content
¶ Content of the response, in bytes.
A CookieJar of Cookies the server sent back.
-
elapsed
= None¶ The amount of time elapsed between sending the request and the arrival of the response (as a timedelta)
-
encoding
= None¶ Encoding to decode with when accessing r.text.
-
headers
= None¶ 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. The list is sorted from the oldest to the most recent request.
-
is_permanent_redirect
¶ True if this Response one of the permanant versions of redirect
-
is_redirect
¶ True if this Response is a well-formed HTTP redirect that could have been processed automatically (by
Session.resolve_redirects()
).
-
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.
If decode_unicode is True, content will be decoded using the best available encoding based on the response.
-
iter_lines
(chunk_size=512, decode_unicode=None, delimiter=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
= None¶ File-like object representation of response (for advanced usage). Use of
raw
requires thatstream=True
be set on the request.
-
reason
= None¶ Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.
-
request
= None¶ The
PreparedRequest
object to which this is a response.
-
status_code
= None¶ Integer Code of responded HTTP Status, e.g. 404 or 200.
-
text
¶ Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
chardet
.The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set
r.encoding
appropriately before accessing this property.
-
url
= None¶ 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.
- url – URL for the new
-
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.
- url – URL for the new
-
requests.
post
(url, data=None, json=None, **kwargs)¶ Sends a POST request. Returns
Response
object.Parameters:
-
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.
- url – URL for the new
Exceptions¶
-
exception
requests.
RequestException
(*args, **kwargs)¶ There was an ambiguous exception that occurred while handling your request.
-
exception
requests.
ConnectionError
(*args, **kwargs)¶ A Connection error occurred.
-
exception
requests.
HTTPError
(*args, **kwargs)¶ An HTTP error occurred.
-
exception
requests.
URLRequired
(*args, **kwargs)¶ A valid URL is required to make a request.
-
exception
requests.
TooManyRedirects
(*args, **kwargs)¶ Too many redirects.