Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 28, 2021 12:50 am GMT

Sending GET, POST, PUT, DELETE Requests In Python

Hello, I'm Aya Bouchiha, today, we'll talk about sending requests in python using the requests module

installation

pip install requests

What's GET request

GET: is a request used for getting or retrieving data or information from a specified server.

Sending GET requests in Python using requests

import requestsurl = 'https://jsonplaceholder.typicode.com/posts/1'response = requests.get(url)# <Response [200]>print(response)# 200print(response.status_code)# {#  "userId": 1,#  "id": 1,#  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",#  "body": "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto"#}
print(response.text)# https://jsonplaceholder.typicode.com/posts/1print(response.url)# application\json; charset=utf-8print(response.headers['Content-Type'])# b'{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\
suscipit recusandae consequuntur expedita et cum\
reprehenderit molestiae ut ut quas totam\
nostrum rerum est autem sunt rem eveniet architecto"
}'
print(response.content)# {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto'}
print(response.json())# <class 'dict'>print(type(response.json()))# for more information# print(dir(response))

What's POST request

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

Sending POST requests in Python using requests

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)

What's the PUT request

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

Sending PUT requests in Python using requests

import requestsimport jsonurl = '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)# 200print(response.status_code)# Trueprint(response.ok)# b'{
"id": 1,
"userId": 2,
"title": "drink water",
"body": "drinking water is important"
}'
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/postsprint(response.url)# application/json; charset=utf-8print(response.headers['Content-Type'])# utf-8print(response.encoding)

What's a delete request

DELETE: is a request used to delete a specific resource in a server.

Sending a DELETE request in python

import requestsimport jsonurl = 'https://jsonplaceholder.typicode.com/posts/1'headers = {'Content-Type': 'application/json; charset=UTF-8'}response = requests.delete(url, headers=headers)print(response.status_code) # 200print(response.ok) # Trueprint(type(response.text)) # <class 'str'>print(response.url) # https://jsonplaceholder.typicode.com/posts/1print(response.headers['Content-Type']) # application/json; charset=utf-8print(response.encoding) # utf-8

Have a good day


Original Link: https://dev.to/ayabouchiha/sending-get-post-put-delete-requests-in-python-45o8

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