A gentle introduction to HTTP for Flask

Share this article

Are you new to working with HTTP requests and looking to learn more about what HTTP requests are? Or maybe you are familiar with HTTP requests but looking for more information on how they relate to Flask? Well then you are in luck! In this article, we discuss everything you need to know to understand what HTTP requests are and how they relate to Flask.  

This article starts out with some background information on what the client server model is and why it is relevant to HTTP requests. After that we discuss the different components of HTTP requests and HTTP responses. Finally, we discuss what Flask is and how Flask relates to HTTP requests.

A diagrammingm of how client server architecture works when HTTP requests are made.

The client server model

Before you can understand HTTP requests, you first need to understand how the client server model works on the web. Generally speaking, a client is a program or machine that is used to make a request through the web such as an internet browser. Clients are often transient and can be turned off when no requests are being made.  

A server, on the other hand, is a program that serves clients by responding to their requests. Servers generally contain data that needs to be accessed by clients. Unlike clients, which are frequently turned on and off, servers are generally kept up and running so they can listen for requests from clients.  

An introduction to HTTP requests

What is a HTTP request?

Hypertext Transfer Protocol (HTTP) is a protocol that facilitates communication between clients and servers. Specifically, a HTTP request is a way for a client to make requests that are then responded to by the server. There are different methods associated with HTTP requests that allow clients to request data from the server, send data to the server, delete data on the server, and more.  

What is in a HTTP request?

HTTP requests contain multiple pieces of information that inform how the server should respond to the client. This information is segmented into three different components. 

  • Request line. The request line contains basic information about what kind of request is being sent and where the request should be routed. Most of the information that appears in the request line is required in order to get the request routed to the server. 
  • Header. The header provides additional metadata about the content of the message. This may include information about the data that is being sent to the server (ex. the file type of the data in the body) or data that is being requested from the server (ex. pull only documents written in English).  
  • Body. Finally, the body contains the main content of the message. This should include any additional data that the server needs to complete the actions specified in the request. For example, if you are making a request in order to send a video file to be stored on the server then the video file should be included in the body. 
A diagram with an example of how client server architecture is structured for HTTP requests.

What is a HTTP request line?

The request line includes crucial information that ensures that the request gets routed to the correct location and the correct actions get taken. 

  • Method. Each HTTP request contains a method which represents the type of request that is being made. Different methods are used to complete different tasks such as downloading data from the server, uploading data to the server, and deleting data from the server. We will talk more about different HTTP methods later. 
  • URL. Each HTTP request will be directed at a specific URL. Different URLS are set up to respond to HTTP requests in different ways, so different responses should be expected when similar requests are sent to different URLs.
  • Version. The specific HTTP version that the request conforms to should also be included in the HTTP request line. 
  • Query parameters (optional). Query parameters may also be included in the request line to provide more information about how to respond to the request. Query parameters generally appear at the end of the URL and are preceded by a question mark. 

What is a HTTP request header?

What information is contained in the HTTP header? In general, the HTTP header contains metadata that provides context about the request. Here are just a few examples of types of data that might be included in a HTTP header.  

  • Metadata about body content. If you are sending a HTTP request to upload data to a server then the HTTP header might contain metadata about the data that is contained in the body of the message. For example, the header might contain information about the type of file that was included in the body of the request. 
  • Conditionals. The header may also include conditional information that informs the server about how to respond to the request. For example, if you are sending out a HTTP request in order to retrieve data from the server then the header might contain information on what types of documents you want to receive. You might include a message in the header that says you are only interested in receiving text documents that are written in the English language, for example.
  • Information about the client. HTTP headers also commonly include information about the client that is being used to make the HTTP request. This can be anything from the screen width of the device that is being used to a unique ID that reveals the identity of the client that is accessing the server. 
  • Authentication. A HTTP header might also contain authentication information, such as the authentication method that should be used for your request. It may even contain credentials that are used in authentication. 

What is a HTTP request body?

What information does the body of a HTTP request contain? The body of a HTTP request contains the main content of the request. Here are some examples of content that might be contained in the body of an HTTP request. 

  • HTML pages. If you are browsing the web and clicking from page to page on a website then the HTTP requests that are being made by your browser are likely returning web pages with heavy HTML components.  
  • Image, video, text files, and more. HTTP body content can return a variety of different file types including image files, video files, and more. Anytime you are downloading a file off of the web, it is likely that the file was included in the body of a HTTP request. 
  • JSON documents. If you are making a HTTP request to a backend API to access data then you may receive a response in the form of a JSON blob. 
  • Nothing at all. HTTP requests that are sent out to fetch information (such as GET requests) often do not contain any body content at all. This is totally fine and expected for multiple different types of requests. 

What is in a HTTP response?

What is in a HTTP response? HTTP responses have three main components that mirror the components HTTP requests. Here are the main components of a HTTP response. 

  • Status line. Just like the request line of a HTTP request, the status line of a HTTP response contains information on the version of the HTTP protocol that the response conforms to. Additionally, the status line generally contains a status code that indicates whether the request was successfully processed as well as a human readable status text that explains the status code. 
  • Header. Just like a HTTP request, a HTTP response has a header that contains metadata about the response. 
  • Body. Just like a HTTP request, a HTTP response contains a body that contains the main content of the request. As with HTTP requests, not all HTTP responses will contain a body. 

What are the most common types of HTTP requests?

The final piece of information that is important for beginners to understand about HTTP requests is the different types of HTTP requests that can be made. Here are some of the most common HTTP methods.

  • GET. The first type of request we will talk about is a GET request. This is a great place to start off because this is one of the most common types of HTTP request. A GET request is made when the client wants to access resources that are stored on the server. The resource is transferred to the client in the response from the server.
  • HEAD. A HEAD request is similar to a GET in that a head request is only made when the client needs to access a resource on the server. A HEAD request differs from a GET request because a HEAD request only returns the header of the response, not the body. 
  • POST. Another common type of request is a POST request. A POST request is made when the client wants to send a resource to be stored on the server. 
  • PATCH. A PATCH request is made when the client needs to send a signal to the server that a resource on the server needs to be modified. Specifically, a PATCH request is made when a partial modification needs to be made to the target resource. 
  • PUT. But what if you want to replace the entire resource rather than just modifying part of it? In this case you would use a PUT request rather than a PATCH request. 
  • DELETE. A DELETE request does exactly what it sounds like. A DELETE request is made when the client needs to signal to the server that a resource that is stored on the server needs to be deleted. 

How does HTTP relate to Flask?

Now that we’ve talked about what HTTP requests are and what functions they serve, we will talk about what Flask is and how HTTP requests relate to Flask. Flask is a web development framework that developers can use to build out the logic that determines exactly what gets returned when a client makes a HTTP request.

Developers can use Flask to define URLs that can be targeted by a HTTP request and what type of HTTP requests can be directed at that URLs. After they do that, they can build out logic to determine exactly how a server should respond to that HTTP request depending on what type of method was used and what data or metadata was included in the request. 

Structuring HTTP requests with Flask

So how do you use Flask to build out the logic determining what should be returned by a given HTTP request? The first thing you need to do to build out a Flask app is import Flask and initiate an app. 

from flask import Flask
app = Flask(__name__)

After you create your Flask app, you can create a function that contains the logic required to determine what response the server should send back for a given HTTP request. The app.route decorator can be used to specify what URL you want this logic to apply to as well as what the applicable methods for this URL are. 

Here is a simple example of what this might look like. In this example, the /predict URL would accept POST requests and return a string that says Use this URL to make a prediction

@app.route('/predict', methods=['POST'])
def predict():
  "Use this URL to make a prediction"

Taking this one step further, here is an example of how to set up the logic such that requests made to the /predict URL actually load a mode and make a prediction. 

from flask import Flask
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    try:
        data = request.get_json(force=True)
        features = data.get('features')
        model_path = data.get('model_path')
        model = model.load('model_path')
        prediction = model.pedict()
        return {'prediction': prediction}
    except:
        return None

Share this article

Leave a Comment

Your email address will not be published. Required fields are marked *