Get and Post Requests: How To Send Them Using Python! [2024]

Making Get and Post Requests is crucial when dealing with APIs and the web. Therefore, keep reading to find out how to send them using Python.

As a developer, you will definitely get in touch with get & post requests at some point.
Because you are reading this article, I assume that you’ve already been at that point asking yourself how to send them yourself.

In the following, I want to show you how you can send post and get requests using Python. Keep reading!

Introduction

What's the difference between GET and POST requests?

Get requests are used to retrieve data from a server, whereas post requests are used to send data to a server. Both types of requests can contain additional arguments. Arguments for get requests get assigned in the URL, whereas arguments for post requests are assigned in its body.

GetPost
  • parameters in URL
  • used for fetching data
  • usually doesn’t make changes to the server
  • parameters in body
  • used for sending/updating data
  • can make changes to the server
Preparation

Obviously, you’re going to need Python and pip installed on your computer. Additionally, you need to install the requests package using pip. To do so, run one of the following commands in your terminal (Terminal for Linux and Mac, or PowerShell/CMD for Windows):

				pip install requests
			

or

				python -m pip install requests
			

That’s everything you need to do before getting to the coding part.

How to send GET requests

Sending a get request in Python is very easy and works with only the following two lines of code:

				import requests
r = requests.get('https://httpbin.org/get')
			

Now, you can also add parameters to the request, as well as other arguments like a timeout in seconds:

				r = requests.get('https://httpbin.org/get', params={'Arg': 'testArg'}, timeout=1.5)
			

Lastly, you can print out the received data and other information about the request:

				print(r.content) # request response content
print(r.text) # response content as a formatted string
print(r.status_code) # response code (e.g. 200)
print(r.url) # complete url (e.g. https://httpbin.org/get?Test=test1)
			

How to send POST requests

Of course, sending a post request to a server is as easy as sending a get request:

				import requests
r = requests.post('https://httpbin.org/post')
			

Again, you can add parameters to the request and pass other arguments to the function:

				r = requests.post('https://httpbin.org/post', params={'Test': 'test1'}, timeout=1.5)
			

Also, you can print the same information as with a get request:

				print(r.content) # request response content
print(r.text) # response content as a formatted string
print(r.status_code) # response code (e.g. 200)
print(r.url) # complete url (e.g. https://httpbin.org/get?Test=test1)
			

Conclusion

Web requests like Post and Get play a big role in our modern world. In fact, a ton of different things wouldn’t work without them. Hence, it is always a good thing to know about those requests as a developer.
And as you can see, sending web requests using Python is very easy and straightforward!

Happy coding, and Thanks for reading!

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *