Resource replacement or update
Updating a resource at a known endpoint.
Requests · Function
Sends an HTTP PUT request and returns the resulting Response. The request body may be supplied with data or json, while other request options are passed as keyword arguments.
requests.put creates and sends an HTTP PUT request to the supplied URL. It can place dictionary, tuple-list, byte, or file-like data in the request body and accepts the optional keyword arguments supported by requests.request, returning the resulting Response.
requests.put(url: _t.UriType, data: _t.DataType = None, **kwargs: Unpack[_t.DataKwargs]) → 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 body of the Request. |
json | JSON-serializable Python object | Optional JSON-serializable Python object to send in the body of the Request. |
**kwargs | Unpack[_t.DataKwargs] | Optional arguments accepted by request. |
Response object (requests.Response).
import requests
response = requests.put(
"https://httpbin.org/put",
data={"key": "value"},
timeout=10,
)
response.raise_for_status()
print(response.status_code)Sends form-style data in a PUT request and checks that the server returned a successful status.
Updating a resource at a known endpoint.
Submitting structured form fields to a PUT endpoint.
Sending serialized application data to an API.
It sends a PUT request and returns a requests.Response object. The URL identifies the destination, while data optionally supplies the request body.
Follow-up: How would you send JSON? Pass a JSON-serializable Python object through the documented json keyword argument.
data may be a dictionary, a list of tuples, bytes, or a file-like object. These values are used as the body of the Request.
Follow-up: Why might a list of tuples be useful? It can represent repeated form-field names without collapsing them into one mapping entry.
The function is a convenience wrapper for the PUT HTTP method. It accepts the same optional request arguments as the general request function, including json when a JSON body is needed.
Follow-up: What should be checked after receiving the response? Inspect the Response or call raise_for_status to surface unsuccessful HTTP status codes.
requests.requestrequests.postrequests.patchrequests.deleterequests.Response