Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 23, 2021 10:41 am GMT

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 requestsimport jsonurl = '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)# 201print(response.status_code)# Trueprint(response.ok)# b'{
"title": "buy new mouse",
"body": "I need to buy a new mouse !",
"userId": 5,
"id": 101
}'
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/postsprint(response.url)# application/json; charset=utf-8print(response.headers['Content-Type'])# utf-8print(response.encoding)

tutorials to learn requests

Suggested posts

Happy coding!


Original Link: https://dev.to/ayabouchiha/sending-a-post-request-in-python-3mgh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To