Master Method: POST PUT DELETE Get Requests In Python

Installation:

pip install requests

 

Sending GET requests in Python using requests

import requests

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

response = requests.get(url)

# <Response [200]>
print(response)

# 200
print(response.status_code)

# {
#  "userId": 1,
#  "id": 1,
#  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
#  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
#}
print(response.text)

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

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

# b'{\n  "userId": 1,\n  "id": 1,\n  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",\n  "body": "quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto"\n}'
print(response.content)

# {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}
print(response.json())

# <class 'dict'>
print(type(response.json()))

# for more information
# print(dir(response))

Sending POST requests in Python using requests

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)

 

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)

 

Sending a DELETE request in python

import requests
import json

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

headers = {'Content-Type': 'application/json; charset=UTF-8'}

response = requests.delete(url, headers=headers)

print(response.status_code) # 200

print(response.ok) # True

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

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

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

print(response.encoding) # utf-8