Learn Requests

The simple, synchronous HTTP client most Python code uses

15 lessons · 3 hours · 4 levels · covers Requests 2.34

Requests is the de facto standard HTTP library for Python: a small, readable API for sending GET, POST and other requests, handling JSON, headers, cookies, authentication and file uploads. It sits on top of urllib3, which provides connection pooling and retries. Learning it lets you call web APIs, download files and script any HTTP service from Python.

Before you start

Requests builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 34 min in total
2. PracticalUse it productively at work, with the usual idioms9 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs12 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know15 lessons · 3 hours in total

The Requests tree

RequestsYour first GET requestYour first GETrequestThe Response objectThe Response objectQuery parameters and headersQuery parametersand headersSending JSON, forms and other bodiesSending JSON, formsand other bodiesErrors and raise_for_statusErrors andraise_for_statusTimeoutsTimeoutsSessions: connection reuse and defaultsSessions: connectionreuse and defaultsAuthenticationAuthenticationTesting code that uses RequestsTesting code thatuses RequestsDictionariesDictionariesModules and importsModules and importsExceptions in depthExceptions in depthContext managers and withContext managersand withTesting with pytest and unittestTesting with pytestand unittestReading and writing filesReading and writingfilesThreads, processes and concurrent.futuresThreads, processesand concurrent.fut…
Essentials and Practical shown (9 of 15 lessons); the full list is below. Click a lesson to read it.

All 15 lessons, in order

Making requests

  1. Your first GET request · Essentials · 7 min

    Install Requests and fetch a URL with requests.get, then look at the status code and body.

  2. Query parameters and headers · Essentials · 8 min

    Pass query strings with params= and custom headers with headers=, letting Requests handle encoding.

  3. Sending JSON, forms and other bodies · Essentials · 10 min

    Send request bodies with json=, data= and files= using POST, PUT and PATCH, and know which Content-Type each sets.

Responses and errors

  1. The Response object · Essentials · 9 min

    Read a response's body as text, bytes or JSON, inspect headers and encoding, and see redirects and timing.

  2. Errors and raise_for_status · Practical · 10 min

    Turn HTTP error statuses into exceptions with raise_for_status and handle the Requests exception hierarchy.

Robust clients

  1. Timeouts · Practical · 8 min

    Always set timeout=, understand connect versus read timeouts, and know what a timeout does not limit.

  2. Retries with HTTPAdapter and urllib3 · Advanced · 12 min

    Configure automatic retries with backoff by mounting an HTTPAdapter with a urllib3 Retry policy on a session.

  3. Streaming downloads and file uploads · Advanced · 12 min

    Download large files without loading them into memory using stream=True, and upload files with multipart or streamed bodies.

  4. TLS verification, certificates and proxies · Advanced · 11 min

    Control certificate verification with verify=, use client certificates, and route traffic through proxies, including environment settings.

Sessions and auth

  1. Sessions: connection reuse and defaults · Practical · 10 min

    Use requests.Session to reuse connections, persist cookies and share headers, auth and other defaults across calls.

  2. Authentication · Practical · 10 min

    Authenticate with Basic auth, bearer tokens and custom AuthBase classes, and keep credentials out of code.

  3. Testing code that uses Requests · Practical · 12 min

    Test HTTP-calling code without a network by mocking responses with the responses library or a session fake.

Internals and extending

  1. Prepared requests and the request lifecycle · Expert · 13 min

    Follow a request from Request to PreparedRequest to adapter, and use prepared requests to inspect or tweak exactly what is sent.

  2. Connection pooling and thread safety · Expert · 12 min

    Tune urllib3 connection pools through HTTPAdapter and use Requests safely from threads.

  3. Event hooks and custom transport adapters · Expert · 13 min

    Run code on every response with hooks, and replace the transport itself by writing and mounting a custom adapter.