Python Post Request with Json File Tutorial

Published On: 18/10/2022 | Category: Python


Hi Guys,

Are you looking for example of python post request with json file. This article will give you simple example of python post request with json file. I’m going to show you about python request json file. I would like to share with you python request with json body. you will do the following things for post request with json file in python.

In this instance, we'll use the requests library to execute all POST HTTP Requests with a json file as an argument and return a JSON response in a python programme. In this example, I'll make a file called parameters.json and use its json arguments. I'll give you a very basic example of how to call Python's POST Request function with body parameters.

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

parameters.json
{
    "name": "Bhavesh", 
    "job": "Developer"
}

Example:

main.py
import requests
import json
  
url = 'https://reqres.in/api/users'
  
headers = {
             'Accept' : 'application/json', 
             'Content-Type' : 'application/json'
             }
 
contents = open('parameters.json', 'rb').read()
response = requests.post(url, data=contents, headers=headers)
  
data = response.json()
  
print(data)
Output
{
    'name': 'Bhavesh', 
    'job': 'Developer', 
    'id': '409', 
    'createdAt': '2022-10-18T04:27:04.623Z'
}

It will help you....

Happy Pythonic Coding!