Sending A POST Request In Python

What’s POST request

POST: is a request that is used for sending information or data to a specific server.

installation

pip install requests

Sending POST requests in Python

import requests
import json

url =…


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

What's POST request

POST: is a request that is used for sending information or data to a specific server.

installation

pip install requests

Sending POST requests in Python

import requests
import json

url = 'https://jsonplaceholder.typicode.com/posts'

data = { 'title': 'buy new mouse','body': 'I need to buy a new mouse !','userId': 5,}
headers = {'content-type': 'application/json; charset=UTF-8'}

response = requests.post(url, data=json.dumps(data), headers=headers)

# 201
print(response.status_code)

# True
print(response.ok)

# b'{\n  "title": "buy new mouse",\n  "body": "I need to buy a new mouse !",\n  "userId": 5,\n  "id": 101\n}'
print(response.content)

# {
#   "title": "buy new mouse",
#   "body": "I need to buy a new mouse !",
#   "userId": 5,
#   "id": 101
# }
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-23T10:41:59+00:00) Sending A POST Request In Python. Retrieved from https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/

MLA
" » Sending A POST Request In Python." Aya Bouchiha | Sciencx - Monday August 23, 2021, https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/
HARVARD
Aya Bouchiha | Sciencx Monday August 23, 2021 » Sending A POST Request In Python., viewed ,<https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/>
VANCOUVER
Aya Bouchiha | Sciencx - » Sending A POST Request In Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/
CHICAGO
" » Sending A POST Request In Python." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/
IEEE
" » Sending A POST Request In Python." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/08/23/sending-a-post-request-in-python/. [Accessed: ]
rf:citation
» Sending A POST Request In Python | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/08/23/sending-a-post-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.