Fetch a resource
Retrieve a web resource or API endpoint using the GET method.
Requests · Function
Sends an HTTP GET request and returns a requests.Response. Query-string data goes in params; other request options are supplied as keyword arguments.
requests.get creates and sends a GET request for the supplied URL. It can add query parameters and accepts the optional keyword arguments supported by requests.request; the returned Response exposes the server's result.
requests.get(url, params=None, **kwargs)| Name | Type | Description |
|---|---|---|
url | UriType | URL for the new Request object. |
params | ParamsType | None | Optional dictionary, list of tuples, or bytes to send in the request query string. |
**kwargs | GetKwargs | Optional keyword arguments accepted by request. |
A Response object; return type requests.Response.
url specifies the resource for which Requests constructs the request.params encodes query-string values and supports mappings, repeated-key tuples, or raw bytes.kwargs provides the optional controls accepted by the general request function.stream=True when you need to defer body retrieval.import requests
response = requests.get(
"https://httpbin.org/get",
params={"page": 2},
timeout=10,
)
response.raise_for_status()
print(response.json())This sends a GET request with query parameters, checks for an HTTP error, and reads the JSON response.
Retrieve a web resource or API endpoint using the GET method.
Add filters, pagination, or other query-string values without manually building the URL.
Apply shared request options such as headers, authentication, timeout, or streaming.
requests.get return?requests.get is a convenience function for issuing an HTTP GET request. It returns a requests.Response object, which contains the server response and the original request.
Follow-up: How would you avoid downloading the body immediately? Pass stream=True, then consume the response and close it when done.
url identifies the resource, while params supplies query-string data. The documentation allows params to be a dictionary, a list of tuples, or bytes.
Follow-up: Why use a list of tuples? It preserves repeated query-key entries, which a plain dictionary cannot represent directly.
Additional keyword arguments are forwarded through the request machinery, so options such as authentication, headers, redirects, streaming, and timeouts can be supplied. A timeout should be provided when an application must not wait indefinitely.
Follow-up: What happens with stream=True? The body is deferred until accessed, and the response must be closed after use.
requests.requestrequests.Session.getrequests.Responserequests.Request