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('', 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):


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:


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