×

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

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Python Num2words

Python: 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 is said...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.