This post discusses two HTTP (Hypertext Transfer Protocol) request methods GET and POST requests in Python and their implementation in Python.
What is HTTP?
HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and a server. A web browser may be the client, and an application on a computer that hosts a website may be the server. So, to request a response from the server, there are mainly two methods:
- GET: To request data from the server.
- POST: To submit data to be processed to the server.
Here is a simple diagram that explains the basic concept of GET and POST methods.
 Now, to make HTTP requests in Python, we can use several HTTP libraries like:
The most elegant and simplest of the above-listed libraries is Requests. We will be using the requests library in this article. To download and install the Requests library, use the following command:
pip install requests
Making a Get request
The above example finds the latitude, longitude, and formatted address of a given location by sending a GET request to the Google Maps API. An API (Application Programming Interface) enables you to access the internal features of a program in a limited fashion. And in most cases, the data provided is in JSON(JavaScript Object Notation) format (which is implemented as dictionary objects in Python!).
Python
Output:
Important points to infer:
PARAMS = {'address':location}
The URL for a GET request generally carries some parameters with it. For the requests library, parameters can be defined as a dictionary. These parameters are later parsed down and added to the base URL or the API endpoint. To understand the role of the parameter, try to print r.url after the response object is created. You will see something like this:
http://maps.googleapis.com/maps/api/geocode/json? address=delhi+technological+university
This is the actual URL on which the GET request is made
r = requests.get(url = URL, params = PARAMS)
Here we create a response object ‘r’ which will store the request-response. We use requests.get() method since we are sending a GET request. The two arguments we pass are URL and the parameters dictionary.
data = r.json()
Now, in order to retrieve the data from the response object, we need to convert the raw response content into a JSON-type data structure. This is achieved by using json() method. Finally, we extract the required information by parsing down the JSON-type object.
Making a POST request
This example explains how to paste your source_code to pastebin.com by sending a POST request to the PASTEBIN API. First of all, you will need to generate an API key by signing up here and then accessing your API key here.
Python
Important features of this code:
data = {‘api_dev_key’:API_KEY,
‘api_option’:’paste’,
‘api_paste_code’:source_code,
‘api_paste_format’:’python’}
Here again, we will need to pass some data to the API server. We store this data as a dictionary.
r = requests.post(url = API_ENDPOINT, data = data)
Here we create a response object ‘r’ which will store the request-response. We use requests.post() method since we are sending a POST request. The two arguments we pass are the URL and the data dictionary.
pastebin_url = r.text
In response, the server processes the data sent to it and sends the pastebin_URL of your source_code which can be simply accessed by r.text.
requests.post method could be used for many other tasks as well like filling and submitting the web forms, posting on your FB timeline using the Facebook Graph API, etc.
Here are some important points to ponder upon:
- When the method is GET, all form data is encoded into the URL and appended to the action URL as query string parameters. With POST, form data appears within the message body of the HTTP request.
- In the GET method, the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64K.No such problem in the POST method since we send data in the message body of the HTTP request, not the URL.
- Only ASCII characters are allowed for data to be sent in the GET method. There is no such restriction in the POST method.
- GET is less secure compared to POST because the data sent is part of the URL. So, the GET method should not be used when sending passwords or other sensitive information.
GET and POST Requests Using Python – FAQs
What is GET and POST Request in Python?
In Python, GET and POST requests are ways to send data to a server or retrieve data from a server using HTTP:
- GET Request: A GET request is used to retrieve data from a server. It sends data appended in the URL and is mainly used for fetching data. It has limitations on the amount of data that can be sent because data is sent in the URL.
- POST Request: A POST request is used to send data to the server, for example, when uploading a file or submitting a completed form. Data sent via POST is transmitted in the body of the request, allowing larger amounts of data to be sent compared to a GET request.
How to Send a POST Request with Python Requests?
To send a POST request using the Python
requests
library, you need to use therequests.post()
method. This method allows you to send data to a server or API and is useful for tasks such as submitting form data or uploading files.import requests
url = 'http://example.com/api'
data = {'key': 'value'}
response = requests.post(url, data=data)
print(response.text)
How Do You POST a Request to a Server in Python?
Posting a request to a server in Python typically involves specifying the URL of the server, organizing the data you wish to send, and then using the
requests.post()
method to send the data. Here’s an example of how to send JSON data:import requests
import json
url = 'http://example.com/api'
data = {'key': 'value'}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(data), headers=headers)
print(response.text)
What Does Requests get()
Do in Python?
The
requests.get()
function in Python is used to send a GET request to a specified url. This function retrieves data from a server at the specified URL and brings it back to the local Python environment. It’s commonly used to access web pages, download files, or consume data from APIs.import requests
url = 'http://example.com'
response = requests.get(url)
print(response.text)
How to API Call in Python?
Making an API call in Python typically involves sending a GET or POST request to the API’s URL. The
requests
library is commonly used for this purpose due to its simplicity and ease of use. Here’s an example of making a GET request to an API:import requests
# Define the API endpoint
url = 'https://api.example.com/data'
# Set any API parameters
params = {
'param1': 'value1',
'param2': 'value2'
}
# Send the GET request
response = requests.get(url, params=params)
# Check the status code and process the response
if response.status_code == 200:
data = response.json()
print(data)
else:
print("Failed to retrieve data", response.status_code)
This example demonstrates how to use requests.get()
to send a GET request with parameters to an API, process the response, and handle errors effectively.