Send GET and POST requests in Python

In this tutorial, we will learn to send get and post requests in Python. The GET and POST are two most used  HTTP(Hypertext Transfer Protocol) methods.

GET and POST requests in Python

To use these methods, there are various libraries available like,

  • urllib
  • httplib
  • requests

In this module, we will use requests for understanding GET and POST.

The requests module

As we know that making even a simple HTTP requests may involve writing a huge amount of code. Introducing a third-party requests library that is used to make all kinds of HTTP/1.1 requests. Written by Kenneth Reitz, it is one of the most used python libraries. You can access it just by importing it. Hence, it is very efficient to use.

Here is the detailed documentation of requests. To use requests, you have to first install in your system using:

pip install requests

Or download it from here.

Next, import requests in your code using:

import requests

For making HTTP requests, two most used methods are:

  1. GET
  2. POST

GET method:

It simply requests data from the server. You can use it for making a request, getting the content or downloading any files from the specified URL. Syntax:

requests.get(url, params)

Let us illustrate this with an example,

import requests

url = "https://web.ics.purdue.edu/~gchopra/class/public/pages/webdesign/05_simple.html"
print(requests.get(url))

Here, URL of a very basic website is taken. Running this code, the output would look something like this:

<Response [200]>

It returns response status code 200. HTTP status codes are server responses to the requests made. 200 simply means that the request has been successful. We can get access to various information and contents of the site using different functions like:

  • header
  • text
  • content
  • json()
  • links
  • cookies, etc

Just type in dir() or help() to get the whole list of methods that can be used. Let us take an example of .content:

import requests

url = "https://web.ics.purdue.edu/~gchopra/class/public/pages/webdesign/05_simple.html"
response = requests.get(url)
print(response.content)

The output will display the content of the response in bytes,

b'<html>\n\n<head>\n<title>A very simple webpage</title>\n<basefont size=4>\n</head>\n\n<body bgcolor=FFFFFF>\n\n<h1>A very simple webpage. This is an "h1" level header.</h1>\n\n<h2>This is a level h2 header.</h2>\n\n<h6>This is a level h6 header.  Pretty small!</h6>\n\n<p>This is a standard paragraph.</p>\n\n<p align=center>Now I\'ve aligned it in the center of the screen.</p>\n\n<p align=right>Now aligned to the right</p>\n\n<p><b>Bold text</b></p>\n\n<p><strong>Strongly emphasized text</strong>  Can you tell the difference vs. bold?</p>\n\n<p><i>Italics</i></p>\n\n<p><em>Emphasized text</em>  Just like Italics!</p>\n\n<p>Here is a pretty picture: <img src=example/prettypicture.jpg alt="Pretty Picture"></p>\n\n<p>Same thing, aligned differently to the paragraph: <img align=top src=example/prettypicture.jpg alt="Pretty Picture"></p>\n\n<hr>\n\n<h2>How about a nice ordered list!</h2>\n<ol>\n  <li>This little piggy went to market\n  <li>This little piggy went to SB228 class\n  <li>This little piggy went to an expensive restaurant in Downtown Palo Alto\n  <li>This little piggy ate too much at Indian Buffet.\n  <li>This little piggy got lost\n</ol>\n\n<h2>Unordered list</h2>\n<ul>\n  <li>First element\n  <li>Second element\n  <li>Third element\n</ul>\n\n<hr>\n\n<h2>Nested Lists!</h2>\n<ul>\n  <li>Things to to today:\n    <ol>\n      <li>Walk the dog\n      <li>Feed the cat\n      <li>Mow the lawn\n    </ol>\n  <li>Things to do tomorrow:\n    <ol>\n      <li>Lunch with mom\n      <li>Feed the hamster\n      <li>Clean kitchen\n    </ol>\n</ul>\n\n<p>And finally, how about some <a href=http://www.yahoo.com/>Links?</a></p>\n\n<p>Or let\'s just link to <a href=../../index.html>another page on this server</a></p>\n\n<p>Remember, you can view the HTMl code from this or any other page by using the "View Page Source" command of your browser.</p>\n\n</body>\n\n</html>\n'

Similarly, we can make different kinds of requests using get method. It is, therefore, a very handy and useful tool in python.

Post method:

This method submits data to the server. It sends data to the body whereas get method uses the URL. Hence, post method is known to be much more secure. Syntax:

requests.post(url, data)

Here is a sample of how you can submit data using post,

url = "https://form.jotform.me/70568615073459"
dict = {'key': 'value'}
response = requests.post(url, data=dict)

You can try out different HTTP methods including get and post using this site,

https://httpbin.org/

Also learn:

Leave a Reply

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