requests — HTTP client¶
The requests module provides a minimal HTTP/HTTPS client API similar to the
Python requests library. Each request
function returns a requests.Response object.
Example:
import requests
# GET a JSON resource.
r = requests.get("https://httpbin.org/get")
print(r.status_code, r.reason)
print(r.json())
# POST JSON.
r = requests.post(
"https://httpbin.org/post",
json={"id": 1, "value": 42},
headers={"X-Source": "openmv"},
)
print(r.json())
Response class¶
- class requests.Response(code: int, reason: str, headers: bytes = None, content: bytes = None)¶
Represents an HTTP response. Instances are returned by
requests.requestand the per-method helpers.- encoding: str¶
String encoding used to decode
requests.Response.headersandrequests.Response.content. Defaults to"utf-8".
- headers: str¶
Response headers decoded with
requests.Response.encodingand returned as astr.
- content: str¶
Response body decoded with
requests.Response.encodingand returned as astr.
- json() dict¶
Parse
requests.Response.contentas JSON and return the resulting object.
Functions¶
- requests.request(method: str, url: str, data: bytes | None = None, json: Any | None = None, files: dict | None = None, headers: dict = {}, auth: tuple | None = None, stream: Any | None = None) Response¶
Send an HTTP request to
urland return arequests.Response.method— HTTP method as astr(e.g."GET","POST").url— Target URL. Must start withhttp://orhttps://.data— Raw request body. If set,Content-Lengthis added automatically.json— Object serialized to JSON and sent as the body. SetsContent-Type: application/json.files— Dict mapping field name to a(filename, fileobj)tuple. Sent asmultipart/form-data.headers— Dict of additional request headers.auth—(username, password)tuple for HTTP Basic authentication.stream— Accepted for API compatibility; not used.
- requests.head(url: str, **kw: Any) Response¶
Send an HTTP
HEADrequest and return aResponse.HEADis identical toGETexcept the server replies with only the status line and headers; the body is empty. Use it to check whether a resource exists, inspectContent-Length/Content-Typewithout downloading the payload, or probe a URL before issuing a heavierGET.Arguments:
url– target URL; must start withhttp://orhttps://.headers(kwarg) – dict of additional request headers.auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
data/json/files/streamfromrequest()are accepted for completeness but rarely make sense forHEAD.
- requests.get(url: str, **kw: Any) Response¶
Send an HTTP
GETrequest and return aResponse.GETis the standard verb for retrieving a representation of the resource identified byurl. It is safe (causes no server-side state change) and idempotent.Arguments:
url– target URL; must start withhttp://orhttps://.headers(kwarg) – dict of additional request headers (for exampleAuthorizationorAccept).auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
A request body via
data/jsonis permitted by the underlyingrequest()but ignored by most servers.
- requests.post(url: str, **kw: Any) Response¶
Send an HTTP
POSTrequest and return aResponse.POSTsubmits data tourl, typically creating a new subordinate resource, triggering a form submission, or invoking an action. It is neither safe nor idempotent: repeated calls may create duplicate resources.Arguments:
url– target URL; must start withhttp://orhttps://.data(kwarg) – raw request body (bytes-like). Sends aContent-Lengthheader automatically.json(kwarg) – object serialised to JSON and sent as the body. SetsContent-Type: application/json.files(kwarg) – dict mapping a field name to(filename, fileobj). Sent asmultipart/form-data.headers(kwarg) – dict of additional request headers.auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
Pass at most one of
data/json/files.
- requests.put(url: str, **kw: Any) Response¶
Send an HTTP
PUTrequest and return aResponse.PUTreplaces the resource aturlwith the supplied representation, creating it if it does not exist. It is idempotent: repeating an identicalPUTyields the same final state.Arguments:
url– target URL; must start withhttp://orhttps://.data(kwarg) – raw replacement body (bytes-like).json(kwarg) – object serialised to JSON and sent as the replacement body. SetsContent-Type: application/json.headers(kwarg) – dict of additional request headers.auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
Pass either
dataorjsonto carry the new representation.
- requests.patch(url: str, **kw: Any) Response¶
Send an HTTP
PATCHrequest and return aResponse.PATCHapplies a partial modification to the resource aturl– only the fields contained in the request body change. UnlikePUT, it is not required to be idempotent (though many APIs make it so).Arguments:
url– target URL; must start withhttp://orhttps://.data(kwarg) – raw delta body (bytes-like). Format depends on the server (e.g. JSON Patch, JSON Merge Patch).json(kwarg) – delta object serialised to JSON. SetsContent-Type: application/json.headers(kwarg) – dict of additional request headers.auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
- requests.delete(url: str, **kw: Any) Response¶
Send an HTTP
DELETErequest and return aResponse.DELETErequests removal of the resource aturl. It is idempotent: deleting an already-deleted resource is not an error (the server typically returns a 404, but the end state is the same).Arguments:
url– target URL; must start withhttp://orhttps://.headers(kwarg) – dict of additional request headers.auth(kwarg) –(username, password)tuple for HTTP Basic authentication.
A body via
data/jsonis allowed by the underlyingrequest()but rarely used withDELETE.