.request()
The .request()
method sends seven main kinds of request to a web server: GET
, OPTIONS
, HEAD
, POST
, PUT
, PATCH
, and DELETE
; it can also handle custom HTTP verbs if needed, and returns a response object.
Syntax
import requests
requests.request("method", "url", **kwargs)
**kwargs
are any number of dictionary items (named arguments) that are passed in as parameters. Many different named parameters can be passed into a request. For example, they can be used to include cookies in the request, set proxies, set user-agents, or set a page timeout.
The requests
module comes with some commonly used convenience methods such as .get()
, .post()
, and etc. Using one of these convenience methods has the same effect as calling the .request()
method directly.
Example
The request()
method returns a response object which contains various types of data such as the webpage text, status code, and the reason for that response.
import requestsresponse = requests.request("POST", "https://codecademy.com")print(f"{response.status_code}: {response.reason}")
The output will look like this:
200: OK
Codebyte Example
The .request()
method can also take in various parameters. These parameters allow a user to send additional information to the web server such as the content type that should be returned, and the user’s authentication.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.