×

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP).

What is HTTP?

HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client and server possible. It acts as a linker between the server and the client.

What is Hypertext?

A text containing links to other texts is referred to as hypertext i.e., the text that on clicking on it takes the user to another page related to that topic. A hypertext is coded with a standard coding language, i.e., Hypertext Markup Language (HTML). These are highlighted in blue color. The term ‘hypertext’ was given by Ted Nelson around 1965.

The hypertext records are interconnected with the help of hyperlinks, which get activated by tapping on that text via mouse click, touchpad, or keypress set. Sometimes, the word ‘hypertext’ is also used to represent images, tables, and other presentational content formats with united hyperlinks.

It is one of the key concepts of the WWW (World Wide Web), in which web pages are written in Hypertext Markup Language (HTML). “The Garden of Forking Paths”, a short story published by Jorge Luis Borge in 1941 is considered to be an inspiration for the notation of hypertext.

Client and Server

If we consider a web browser as a client and the website hosted by an application on the PC as a server, then we have two methods to make our request and get a response from the server:

1. GET Request Method:

The GET Request Method simply redeems particulars from the server. It is used to request data. The user can pass additional data as a part of this request method to define the query more clearly.

2. POST Request Method:

The POST Request Method is an HTTP request method used by World Wide Web (WWW). It is used to submit the data to the server that is to be processed. This method requests a server to accept the data to be processed or for storing it.

We have to use different HTTP libraries to make HTTP requests in Python. Some of them are:

  • httplib
  • urllib
  • requests

Among these libraries, ‘Requests’ is the most simple and elegant, and the same will be used for writing Python codes. To install this library, the below-given command needs to be executed in the command prompt:

pip install requests

Example for GET Request:

#Importing the requests library
import requests as rq


request = rq.get('http://www.javatpoint.com/')


# Requesting page encoding from the server
e = request.encoding
print("Encoding: ",e)


# Requesting response code from the server
s = request.status_code
print("Response code: ",s)


# Requesting response time from the server
t = request.elapsed
print("Response Time: ",t)


# Requesting header from the server
hdr = request.headers['Content-Type']
print("Header: ", hdr)


# Requesting text from the server
rqt = request.text
print("A small portion of text from the web page:", rqt[0:500])

Output:

GET and POST requests using Python

In the above given example, Encoding, Response code, Response Time, Header, and a portion of code is requested from a website and we have successfully got that information.

Example for POST Request:

import requests as rq
cd_frg = """#Importing the requests library
import requests as rq


request = rq.get('http://www.javatpoint.com/')


# Requesting page encoding from the server
e = request.encoding
print("Encoding: ",e)


# Requesting response code from the server
s = request.status_code
print("Response code: ",s)


# Requesting response time from the server
t = request.elapsed
print("Response Time: ",t)


# Requesting header from the server
hdr = request.headers['Content-Type']
print("Header: ", hdr)


# Requesting text from the server
rqt = request.text
print("A small portion of text from the web page:", rqt[0:500])"""
req = rq.post('https://httpbin.org/post',data = cd_frg)
print(req.text)

Output:

GET and POST requests using Python

In the above example, we have requested a server to store the data and display it. The data that we have requested to store was the code written before to explain the GET request method, thus requests to store any kind of data can be made using the POST request method.

Other than this, the request.post method can also be used to perform other tasks such as filling up and submitting online forms, posting on our social media timeline with the help of that particular social media Graph API (Application Programming Interface), and many more.

Some Points to be kept in mind:

  • While using the GET request method, every bit of data is concealed into a URL, and it is attached in the form of query string parameters, to the action URL. On the other hand, using the POST request method, the form data is provided within the message body of the HTTP request.
  • While using the GET request method we are only allowed to send data in form of ASCII characters. On the other hand, using the POST request method, no such restrictions are there.
  • The POST request method is considered more secure as compared to the GET request method, because data to be sent is the part of the URL, so as a result, we cannot send sensitive information using the GET request method.
  • The amount of data that we can stuff into the URL while using the GET request method is limited to 2K, while some of the servers are able to handle up to 64K. And in the POST request method, there are no such restrictions, as the data is sent in the message body of the HTTP request.

Related Topics

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.