Python Requests Module

ยท

2 min read

Functions for Making Requests:

  1. requests.request(): Sends a request to a specified URL with customizable parameters.

  2. requests.get(): Sends a GET request to the specified URL.

  3. requests.post(): Sends a POST request to the specified URL with data in the request body.

  4. requests.put(): Sends a PUT request to the specified URL.

  5. requests.delete(): Sends a DELETE request to the specified URL.

  6. requests.head(): Sends a HEAD request to the specified URL.

  7. requests.options(): Sends an OPTIONS request to the specified URL.

  8. requests.patch(): Sends a PATCH request to the specified URL.

Utility Functions:

  1. requests.session(): Creates a session object for managing and persisting parameters across multiple requests.

  2. requests.cookies(): Represents the cookies sent in the request.

  3. requests.utils(): Utility functions for working with URLs and query strings.

  4. requests.exceptions: Module containing exceptions raised by requests.

  5. requests.adapters: Module containing classes for managing connection pools and retries.

  6. requests.models(): Contains models used by requests, like Response, Request, etc.

  7. requests.hooks: Module to register hooks for certain events in the request lifecycle.

Classes and Objects:

  1. requests.Response(): Represents a response from an HTTP request, with properties like status_code, text, content, etc.

  2. requests.Request(): Represents a HTTP request to be sent, allowing for fine-grained control.

  3. requests.Session(): Represents a session for sending requests, maintaining settings, and managing cookies.

  4. requests.PreparedRequest(): Represents a pre-prepared request object, ready to be sent via Session.send().

Request and Response Properties:

  1. response.status_code: Property that returns the HTTP status code of the response.

  2. response.text: Property that returns the response content in Unicode.

  3. response.content: Property that returns the response content in bytes.

  4. response.json(): Method that parses the response content as JSON.

  5. request.url: Property that returns the URL of the request.

  6. request.headers: Property containing the request headers.

These functions, classes, and properties cover the complete functionality of the requests module in Python, allowing for versatile HTTP request handling, response processing, and efficient management of sessions and connections.

Did you find this article valuable?

Support BigSmoke's Blog by becoming a sponsor. Any amount is appreciated!

ย