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 6 methods. They all return a Response object.

requests.head(url, **kwargs)

Sends a HEAD request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • headers – (optional) Dictionary of HTTP Headers to sent with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • auth – (optional) AuthObject to enable Basic HTTP Auth.
  • timeout – (optional) Float describing the timeout of the request.
  • allow_redirects – (optional) Boolean. Set to False to disable redirect following.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
requests.get(url, **kwargs)

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • headers – (optional) Dictionary of HTTP Headers to send with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • auth – (optional) AuthObject to enable Basic HTTP Auth.
  • timeout – (optional) Float describing the timeout of the request.
  • allow_redirects – (optional) Boolean. Set to False to disable redirect following.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
requests.post(url, data='', **kwargs)

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary or bytes to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to sent with the Request.
  • files – (optional) Dictionary of ‘filename’: file-like-objects for multipart encoding upload.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • 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 redirect following is allowed.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
requests.put(url, data='', **kwargs)

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary or bytes to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to sent with the Request.
  • files – (optional) Dictionary of ‘filename’: file-like-objects for multipart encoding upload.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • 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 redirect following is allowed.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
requests.patch(url, data='', **kwargs)

Sends a PATCH request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary or bytes to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to sent with the Request.
  • files – (optional) Dictionary of ‘filename’: file-like-objects for multipart encoding upload.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • 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 redirect following is allowed.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
requests.delete(url, **kwargs)

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • params – (optional) Dictionary of parameters, or bytes, to be sent in the query string for the Request.
  • headers – (optional) Dictionary of HTTP Headers to sent with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • 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 redirect following is allowed.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
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)

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.

class requests.models.Response

The core Response object. All Request objects contain a response attribute, which is an instance of this class.

cookies = None

A dictionary of Cookies the server sent back.

error = None

Resulting HTTPError of request, if one occured.

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.

ok = None

True if no error occured.

raise_for_status()

Raises stored HTTPError or URLError, if one occured.

request = None

The Request that created the Response.

status_code = None

Integer Code of responded HTTP Status.

url = None

Final URL location of Response.

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.models.AuthenticationError

The authentication credentials provided were invalid.

exception requests.models.URLRequired

A valid URL is required to make a request.

exception requests.models.InvalidMethod

An inappropriate method was attempted.

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.

Classes

class requests.models.Request(url=None, headers={}, files=None, method=None, data={}, params={}, auth=None, cookiejar=None, timeout=None, redirect=False, allow_redirects=False, proxies=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}).

headers = None

Dictonary of HTTP Headers to attach to the Request.

method = None

HTTP Method to use. Available: GET, HEAD, PUT, POST, DELETE.

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.

Requests is an elegant and simple HTTP library for Python, built for human beings. You are currently looking at the documentation of the development release.

Table Of Contents

Related Topics

Fork me on GitHub