Sending PUT Request In Python

What’s the PUT request

PUT: is a request used for creating or updating a resource in a specific server.

installation

pip install requests

Sending PUT requests in Python using requests

import requests
impor…


This content originally appeared on DEV Community and was authored by Aya Bouchiha

What's the PUT request

PUT: is a request used for creating or updating a resource in a specific server.

installation

pip install requests

Sending PUT requests in Python using requests

import requests
import JSON

url = 'https://jsonplaceholder.typicode.com/posts/1'
data = {'id':1, 'userId':2, 'title':'drink water', 'body':'drinking water is important'}
headers = {'Content-Type':'application/json; charset=UTF-8'}
response = requests.put(url, data=json.dumps(data), headers=headers)


# 200
print(response.status_code)

# True
print(response.ok)

# b'{\n  "id": 1,\n  "userId": 2,\n  "title": "drink water",\n  "body": "drinking water is important"\n}'
print(response.content)

# {
#   "id": 1,
#   "userId": 2,
#   "title": "drink water",
#   "body": "drinking water is important" 
# }
print(response.text)

# <class 'str'>
print(type(response.text))

# https://jsonplaceholder.typicode.com/posts
print(response.url)

# application/json; charset=utf-8
print(response.headers['Content-Type'])

# utf-8
print(response.encoding)

tutorials to learn requests

Suggested posts

Happy coding!


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-08-25T23:30:26+00:00) Sending PUT Request In Python. Retrieved from https://www.scien.cx/2021/08/25/sending-put-request-in-python/

MLA
" » Sending PUT Request In Python." Aya Bouchiha | Sciencx - Wednesday August 25, 2021, https://www.scien.cx/2021/08/25/sending-put-request-in-python/
HARVARD
Aya Bouchiha | Sciencx Wednesday August 25, 2021 » Sending PUT Request In Python., viewed ,<https://www.scien.cx/2021/08/25/sending-put-request-in-python/>
VANCOUVER
Aya Bouchiha | Sciencx - » Sending PUT Request In Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/25/sending-put-request-in-python/
CHICAGO
" » Sending PUT Request In Python." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/08/25/sending-put-request-in-python/
IEEE
" » Sending PUT Request In Python." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/08/25/sending-put-request-in-python/. [Accessed: ]
rf:citation
» Sending PUT Request In Python | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/08/25/sending-put-request-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.