Create a POST request
Submit form fields or raw request content to an HTTP endpoint.
Requests · Function
Sends an HTTP POST request and returns a Response. Supply the body with data or with a JSON-serializable value via json, while other request options are passed as keyword arguments.
requests.post creates and sends an HTTP POST request to the supplied URL. The request body can be provided through data or as a JSON-serializable object through json; additional request options are forwarded through keyword arguments, and the call returns a Response.
requests.post(url: _t.UriType, data: _t.DataType = None, json: _t.JsonType = None, **kwargs: Unpack[_t.PostKwargs]) → Response| Name | Type | Description |
|---|---|---|
url | _t.UriType | URL for the new Request object. |
data | _t.DataType = None | Optional dictionary, list of tuples, bytes, or file-like object to send in the request body. |
json | _t.JsonType = None | Optional JSON-serializable Python object to send in the request body. |
**kwargs | Unpack[_t.PostKwargs] | Optional arguments accepted by request. |
Response object
import requests
response = requests.post(
"https://httpbin.org/post",
json={"name": "Ada", "active": True},
)
response.raise_for_status()
print(response.json()["json"])
Sends a JSON-encoded POST body and reads the returned JSON response.
Submit form fields or raw request content to an HTTP endpoint.
Send structured application data without manually serializing it.
Upload a stream or file-like body.
It sends a POST request and returns a requests.Response object. url identifies the destination, while data and json are optional body inputs.
Follow-up: What happens when both json and data are supplied? The documentation says json is ignored when either data or files is passed.
data accepts a dictionary, list of tuples, bytes, or file-like object for the request body. json accepts a JSON-serializable Python object and handles JSON encoding.
Follow-up: How would you upload a large body without reading it all into memory? Pass a file-like object as data, which supports streamed uploads.
Keyword arguments are optional arguments accepted by request. The function returns a Response, whose status, headers, and body can then be inspected.
Follow-up: How should an application handle an unsuccessful HTTP status? Inspect the Response or call raise_for_status() so unsuccessful statuses raise an HTTPError.
requests.requestrequests.getrequests.putrequests.patchrequests.deleterequests.optionsrequests.headrequests.Response