Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

Python Tutorials: How To Send A POST Request Using Python

Example response from server
An example response after parsing it as shown above.

By Nicholas Brown – Follow me on X.

You can send a POST request using Python to a server quite easily using the ‘requests’ library that is already included in Python’s standard libraries. ‘requests’ enables you to send HTTP POST requests as well as other types of HTTP requests using few lines of code.

This tutorial will explain how to send an HTTP POST request from your Python app and how to interpret the server’s response using Python Dictionaries or ‘dicts’.

The first step is to create a Python app called ‘testrequest.py’. Ensure that you have Python 3 installed on your computer.

Python POST Request Example

The first line of code you should type is the import statement that enables you to use the requests library:

import requests

For the second line of code, you will create a request object as follows:

myreq = requests.post('http://127.0.0.1:8080/test', json={'Name': 'Scruffy', 'Species': 'Bumblebee', 'Genus': 'Bombus'})

The code above defines an object called ‘myreq’ and it will include the parameters of our request, as well as the body. The portion of the code starting with ‘json=’ and ending with curly brackets defines the body of our POST request. The body contains a single record containing the name, species and genus of a bee.

The body of our Python POST request is in the JSON format, which is why it says ‘json=’. I chose JSON because it is a broadly used and convenient way to format data so that apps can parse it easily.

Now for the last line of code (remember to insert an empty line at the end):

print(myreq.json()["text"])

This last line of code prints the response received from the server after you sent it the request above. The ‘.json()’ portion indicates that you are expecting the server response to be in the JSON format. ‘[“text”]’ is a field I used to try out parsing. For now, remove ‘[“text”]’ and just run the following to see the entire response from the server:

print(myreq.json())

Re-add the ‘[“text”]’ part and set up a test server to send a response containing the following JSON object:

{text: 'test response'}
Example response from server
An example response after parsing it as shown above.

Related Articles

Python Tutorials: Construct A For Loop In Python

How To Create A Python Environment Using Conda

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published