GET and POST requests using Python

GET and POST requests using Python

This article mainly aims to highlight the two majorly used requests of (Hypertext Transfer Protocol) i.e. GET and POST. We will see its implementation using Python and make use of available libraries like urllib3, requests, and flask.

Introduction

GET and POST are the two major requests on how a web browser works. These requests are made between the client and the server. This can be viewed visually here:

Image by author
APIs request-response model

In the above picture: the Web app is the client. We can see the request and response marked, as our primary area of interest. Whenever we talk about the request-response model, we come across protocols. Protocols are a set of rules that are pre-defined and work based on the rules. So one such protocol is the HTTP(s) that is discussed below.


What is HTTP?

HTTP stands for Hypertext Transfer Protocol or sometimes HTTPS, i.e. Hypertext Transfer Protocol Secured. It is used to transfer data over the web. HTTP uses a client-server model.

Client: Examples are mobile, laptop, computer, etc.

Server: The HTTP server is typically a web host running web server software.

When you access the website, the browser sends a request to the corresponding web server, and it responds with an HTTP status code. If the URL is valid and the connection is granted, the server will send your browser the webpage and its related files.

Some common HTTP status codes include:

Status CodeResponse
200Successful response
400Bad request
404URL not found
405Method not allowed
500Internal server error
HTTP status codes

You can find more about the status codes in the MDN documentation.


Client-server information is transferred using two methods:

GET method/request

  • Used to request data from the server
  • Sometimes retrieves data from the API, which in turn redirects to the server.

POST method/request

  • Used to submit new data for the server to process.
  • Similar to adding new data to the API.

Python urllib3

urllib3 is an intensive, friendly HTTP Client for Python. Urllib3 has many features that most of the standard libraries don’t provide. These include:

  • Thread safety
  • connection pooling
  • TLS/SSL certificate verification
  • 100% test coverage etc.

In real life use case, most of the Python environment already uses urllib3 and checkout this section of the urllib3 documentation that gives much more details.

Python requests

requests is similar to urllib3 but is very simple, clean, and elegant HTTP library for Python. There is no need to write separate code to keep connection alive, pooling, etc, because requests in its core uses urllib3 and therefore requests can be referred to as built on the top of urllib3. Hence, we get most of the features that urllib3 provides and others like:

  • Basic/Digest Authentication
  • Automatic content decoding
  • Chunked Requests, etc.

More features can be found under the Beloved Features section of the requests documentation.

Python Flask

Flask is a web framework for building apps in python. It’s small, light weight, and simple compared to other frameworks like Django. We can easily scale flask to work for complex applications as well.

It provides various features like:

  • Pre-built templates
  • Handling application errors
  • Class-based views
  • request context, etc.

More features are listed in the documentation of the flask.


Before proceeding further, make sure that you have installed the urllib3 library in your environment by running the command in the terminal:

pip install urllib3Code language: Bash (bash)

If you have any difficulties in installation, refer to the urllib3 installation documentation.

GET request with urllib3 in Python

We make use of the urllib3 library available in python to perform the HTTP requests.

Here we are performing the GET request, which accepts the URL as another parameter.

import urllib3

http = urllib3.PoolManager()

URL='http://httpbin.org/ip'

response=http.request('GET', URL)
print(response.data.decode('utf-8'))
Code language: Python (python)

The output is shown below:

# Sample output
"origin": "255.255.255.255"Code language: Python (python)

POST request with urllib3 in Python

Here we are performing the POST request which accepts the URL as another parameter. This is similar to adding new data to the host server. We have imported another library certifi as the URL we pass requires this and can be installed by running the command:

pip install certifiCode language: Bash (bash)

After the installation, if we run the above code, then we would have the JSON response as output to signify that the request has been executed successfully.

import urllib3
import certifi

http=urllib3.PoolManager(ca_certs=certifi.where())
URL='https://httpbin.org/post'

request=http.request('POST', URL, fields = {
    'name': 'Codedamn',
    'highlight' : 'Build projects, practice and learn to code from scratch - without leaving your browser.'
})

print(request.data.decode('utf-8'))
Code language: Python (python)

The output for the above code is shown below:

# Sample output
JSON responseCode language: Python (python)

Before proceeding further, make sure that you have installed requests the library in your environment by running the command in the terminal:

pip install requestsCode language: Bash (bash)

If you have any difficulties in installation refer to the requests installation documentation.


GET request with requests in Python

Here we are making use of requests the module available in python. We utilize the get method available to request the target URL and fetch the response. Here’s the implementation of the same. We make use of the freely available weather API to fetch the results.

import requests

URL='https://api.open-meteo.com/v1/forecast?latitude=12.97&longitude=77.59&current_weather=true'

r = requests.get(url=URL)
data = r.json()

print('Current weather: ' + str(data['current_weather']['temperature']) + ' C')
Code language: Python (python)

The output for the above code is shown below:

# Sample output
Current weather: 21.2 CCode language: Python (python)

Here we have passed the latitude and longitude of the desired location to get the current-weather details. Also, we have set the parameter current-weather to true to get the response from the URL. We use this method to get the response in JSON format and then do the basic python manipulation to get the required data.

POST request with requests in Python

We make use of the same requests module, but we indeed use a different method, i.e. POST that is used to send the data to the server and get a response message. Here’s the implementation of the same:

import requests

data = {
    'name' : 'Codedamn'
}

response = requests.post("https://httpbin.org/post", data)
print(response.text)
Code language: Python (python)

The output of the above code is shown below:

# Sample output
JSON responseCode language: Python (python)

Before proceeding further make sure that you have installed flask library in your environment by running the command in the terminal:

pip install flaskCode language: Bash (bash)

If you have any difficulties in installation refer to the flask installation documentation.


GET request processed in Flask by Python

In this method, we perform the same operation as any other GET request, but we use our server to make the requests. We make use of the Flask module in Python to create our server and perform both methods. Let’s see it in action:

# Flask application
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "App is running"

@app.route('/courses', methods=['GET'])
def courses_list():
    courses = [
        {
            'id': 1,
            'title': 'Go Programming Bootcamp',
            'rating': '5.0',
        },
        {
            'id': 2,
            'title': 'Web Scraping Masterclass With Scrapy and Python',
            'rating': '4.5',
        },
        {
            'id': 3,
            'title': 'Learn Tailwind CSS 3 - A utility-first CSS framework',
            'rating': '4.7'
        }
    ]

    return courses

@app.route('/course/<id>', methods=['POST'])
def course(id):
    return 'Course ' + str(id) + ' created'
Code language: Python (python)

Here initially we import the Flask method available in the flask library which helps us in creating a new flask app. Then we create routes for the app.

The first route is like the home page and then simply returns the response as the App is running to make sure of the presence of the Flask server.

Then the second route we pass the GET method to get the list of courses. Here we just make use of data that we created, but in real time we will make use of databases and real-time systems. The output response is just the JSON output of courses, which is similar to a list of dictionary data types.

POST requests processes in Flask by Python

The final and third route is the POST method, where we pass the ID as a parameter in the URL, and we just post that the new course with the particular ID is created. In real-time, we use the post method to create, authenticate, authorize, etc.

Here are the screenshots of the output of all the routes:

Initial route
Route-1
Post method
Route-3

Conclusion

  • Covered all three major methods of how the GET and POST requests can be handled.
  • Different libraries consumed are urllib3, requests, and flask.
  • Out of all, it is preferred to use requests if we need to handle web-based APIs. If you want to create yown API, in the future deploy it then it is recommended to go with flask.
  • Here is the link to the codedamn playground with all the code used above.

FAQs (Frequently asked questions)

What is GET and POST request in Python?

They are requests or methods available in Python which can be used to GET data from the server or POST new data to the server to process.

How do GET and POST requests work in Python?

GET method sends a request access from the client when requested and similarly POST method sends the data provided by the client to the server to further process.

What is the purpose of Python GET and POST requests?

The purpose of GET and POST requests/methods is to perform the required action requests by the client as these methods act as an intermediate between the client and server dealing with responses from the requests.

Sharing is caring

Did you like what Sriniketh J wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: