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, config=None)
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.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.
  • **kwargs

    Optional arguments that request takes.

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.
  • **kwargs

    Optional arguments that request takes.

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.
  • **kwargs

    Optional arguments that request takes.

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.


class requests.Response

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

config = None

Dictionary of configurations for this request.

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 occurred.

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.

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 occurred.

raise_for_status()

Raises stored HTTPError or URLError, if one occurred.

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.
requests.utils.get_encoding_from_headers(headers)

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 – Response object to get unicode content from.

Tried:

  1. charset from content-type
  2. every encodings from <meta ... charset=XXX>
  3. 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.RequestException

There was an ambiguous exception that occurred 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.TooManyRedirects

Too many redirects.

Classes

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

Authentication tuple to attach to Request.

config = None

Dictionary of configurations for this request.

cookies = 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

Dictionary 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 describes 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