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:
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])
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)
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.