How to POST JSON data with Python Requests Library?

0

Sending a request to the server is a very frequent task that you should be doing when interacting with websites. I am a web scraping expert and I have to send different types of HTTP requests to different websites.

Many of them need data from the clients so that they can recognize the request. In this case, you might need to post data using the JSON format. The JSON format is a very common format to send data to the server.

In this quick python tutorial, I will show you how to send different types of JSON data with python requests. I will show you the working python code that you can use to send JSON data with a post request. So let's get started.

send json data to server in requests

What is an HTTP request?

In normal life, the request is the act of asking for something. Just like the request, an HTTP request is a client asking a host which is located on a server. An HTTP request is nothing but asking for resources from the server.

Picture explaining http request to a web server
Photo BY CDNFinder

Types of HTTP requests

There are different types of HTTP requests, that we can send to a host on a server. Each HTTP request has its own purpose. E.g GET request, Post request, Delete request and so many others. We will discuss the two most popular requests in this python blog.

GET request

When we need to grab some data from a host on a server we use the GET request. We can make a GET request using the get function. Normally the word 'get' is in upper case, just to add it is not an acronym for anything it is just the convention. In python to make a get request, we use the get() function. get() the function is available in the requests library.

Example of GET request in Python

import requests
url = 'https://www.google.com'
res = requests.get(url)
print(res.content)

POST request

A POST request is used when the web server accepts the data enclosed in the body of the request message. A good example of a post request will be submitting data or a file to a web server. Or maybe submit a query to the web server so that it processes the query. Yet another example can be submitting an auth token for authentication purposes. These all are examples of POST requests.

You probably have a good idea, as you are reading this python blog, you are submitting JSON data to a web server. It is up to the server to process the data and return us the response required. Normally the POST request encapsulates the data into a request header.

Request header

A request header is nothing but it is an HTTP header that can be used in an HTTP request to provide information about the request context so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.


Post request in python

In python, there are a lot of methods to make an HTTP POST request to a web server. The most popular and frequently used method for Making a post request is to use the requests library. The requests library is a high-level interface on the urllib module. requests library in python makes it a lot easier to make a post request in python. To make a post request in python, use the post() function. post() function is available in the requests library. You can install the requests library using any python package manager.

Example of an HTTP Post request in Python

import requests
url = 'https://www.example.com'
res = requests.post(url, key="value")
print(res.status_code)


POST JSON data with Python Requests Library

To POST JSON data to a web server using the requests.post() function, with an extra parameter, JSON. With json parameter, you can send the JSON data to a web server using the python requests library. The json parameter takes a python dictionary so make sure to first convert your JSON data into a python dictionary.

Follow these steps to POST JSON data with the Python requests library

  1. Install requests library

    Make sure you have installed the requests library. Installation of the requests library is important because it is a 3rd party library. so before using requests, make sure you install it.

  2. Convert your JSON data to a Python dictionary

    As discussed earlier, the post() function of the requests library accepts the dictionary as a value to json parameter. SO make sure you convert your JSON data into a python dictionary.

  3. import the requests library

    Before using the post() function, you need to import the library. In this case, it is the requests library. SO make sure you import it using the code 'import requests'

  4. Pass the JSON data to post() function

    Now, you are all set. Just pass the JSON data as a python dictionary to the post() function along with the URL.

  5. Verify the status

    Now, once you have made the post request to the web server and sent the JSON data, do you want to verify the status of the response? you can use the res.status_code attribute of the response. It will tell you the status of the response. If it is 200 then you have successfully made the post request to your web server.

Python code Example to POST JSON data with Python requests library

import requests
import json
# Opening JSON file
with open('sample.json') as json_file:
    # type will be dict automatically
    json_data = json.load(json_file)

url = 'http://httpbin.org/post'
res = requests.post(url, json=json_data)
st_code=res.status_code
res_json = res.json()

print(st_code)
print(res_json)

Data in the JSON file is following

You can copy this JSON data and paste it into a new file if you want to taste the code. just make sure the file name is sample.json.

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}


Send JSON data along with Header in Python

Yet another way to send JSON data to a web server is to specify to the server that we are sending out the JSON data we can use the header as well. This way the server will read the header and will understand that the data coming up as JSON data and will accept it if there is a process available on the web server.

import requests
import json
# Opening JSON file
with open('sample.json') as json_file:
    # type will be dict automatically
    json_data = json.load(json_file)

url = 'http://httpbin.org/post'
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
res = requests.post(url, json=json_data, headers=headers)
st_code=res.status_code
res_json = res.json()

print(st_code)
print(res_json)

It is just the same code, but this time we have added the header as well. this is just an example of how you can send a header with a post request to a web server. You can insert any type of information in the header. So this was another possibility.


Send file and JSON data with python requests library

To send a file to a web server in a post request, use the post() function from the requests library and pass the path of the file that you want to send. You can also add the JSON to the parameter list as well. Check out the below code.


import requests
import os

my_file = open(filename, 'rb')
file = {
        'upload': (
            os.path.basename(filename), my_file ,
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        )
    }
data = {
        'uuidKey': str(user_key),
        'agree-term': False,
        'is_tarh': is_incentive_traffic_zone,
        'description': ''
    }

result = requests.post("http://127.0.0.1:5000/schedule-payment", data=data, files=file)


Send JSON with data payload using POST request in Python

You can also send the payload to a server using a POST request in python. With the requests library in python, use the post() function and pass the payload as a data parameter. Check out the following code that sends the data as a payload. It also uses the python warning library which is a very helpful python library for issuing the warning, when something gets wrong.

import requests
import warnings

warnings.filterwarnings("ignore")
term=24
amount=5000
url = 'https://example.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ',
'Cookie':'ASP.NET_SessionId=fhkyn1vn5knlw3uhdnh50nii;'}
payload = {
    "CCRDCalculateInput":{},
    "MultifunctionsCalculateInput":{},
    "Proponents":[{
        "Position":'1',
        "Birthday":"1992-03-10T13:03:24.000Z",
        "ExpenseCodes":[
            "009"]}],
    "Counterparts":'0',
    "ResidualValue":'0',
    "InterestOnly":'0',
    "Deferment":'0'}
jsonData = requests.post(url, headers=headers, json=payload, verify=False).json()
results = jsonData['Result']
mtic = results['MTIC']
installment = results['PeriodInstallment'][0]['Installment']
taeg = results['TAEG']
tan = results['PeriodInstallment'][0]['TAN']


print(f'Installment: {installment}\nTAEG: {taeg}\nTAN: {tan}\nMTIC: {mtic}')


Have a Chat With Me!

Don't hesitate to leave a message at haxratali0@gamil.com. I respond to every mail I receive. I will be more than happy to chat with you.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !