.put()
The .put()
method sends a PUT
request to a web server and returns a response object.
Syntax
import requests
requests.put("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 PUT
request. For example, they can be used to include cookies in the request, set proxies, set headers, or set a page timeout.
Although similar to a POST
request, PUT
requests are idempotent, meaning that multiple requests have the same result. Multiple PUT
requests will overwrite the same resource, and multiple POST
requests will create a new resource each time. Therefore, PUT
is generally used for update operations, and POST
for create operations.
Example
The .put()
method can take in various parameters. These parameters allow a user to communicate additional information to the web server, such as data or JSON to send in the request body in order to create or update a resource.
import requestsjson = {"my_key": "Hello, World!"}response = requests.put("https://httpbin.org/put", json=json)print(response.json()["json"])
The following output shows the json
dictionary that was defined earlier:
{'my_key': 'Hello, World!'}
Codebyte Example
The response object returned by the .put()
method contains various types of data, such as the webpage text, JSON (if returned), status code, and the reason for that response:
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.
Learn Python on Codecademy
- Skill path
Analyze Data with Python
Learn to analyze and visualize data using Python and statistics.Includes 8 CoursesWith CertificateIntermediate13 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours