Quick Guide To HTTP Requests Breaking Things Down Faster With Python
Quick Guide To HTTP Requests Breaking Things Down Faster With Python
Josh Wenner
Stories from my travels and the lessons that have shaped me living in Asia for the last seven
years.
Knowing about HTTP requests is like speaking the internet's language fluently. Whether
you're getting data from an API, sending a form, or uploading files, HTTP requests are like
the messages passed between your web browser and the servers out there.
HTTP, short for Hypertext Transfer Protocol, is like the main road for communication on the
internet. It's what web browsers use to ask for and receive web pages, images, scripts, and
other stuff from web servers.
👉 If you get value from my work, please leave it a ❤️. This helps more people discover this
newsletter on Substack, which helps me out and shows me you enjoy content like this!
In this guide, we're going to really dig into HTTP requests, looking at all the little details and
showing you lots of examples using Python and Flask.
1/10
Each week, I dive deep into Python, breaking it down into bite-sized pieces. But here's the
kicker – while everyone else gets just a taste, my premium readers get the whole feast! Don't
miss out on the full experience – join us today!
Let’s keep in mind for this article that a lot of this is trivial as I break down the basics of HTTP
requests. I cam using examples I made up that I feel can be easily understood by you guys
to further your development.
I covered HTTP requests in a detailed breakdown during our Project X project we did
together last week! We built and interactive Flask CRUD application from start to finish
using Flask, Sass and SQL Alchemy. Check it out here if you missed it!
This is only a chunk of what the full article contains, with less jokes and analogies. If you
want to learn more to further your understanding, check it out here.
Basics of HTTP
Alright, let's break down the basics of HTTP, or Hypertext Transfer Protocol. Think of it as the
highway system of the internet, guiding the flow of information between your web browser
and servers all around the globe.
Picture HTTP like a busy courier service. When you click a link, fill out a form, or do anything
on a web page, you're basically giving instructions to this courier service.
2/10
You're asking it to fetch information from servers and bring it back to your browser, or you're
sending data from your browser to be stored or processed somewhere else on the web.
It's this back-and-forth exchange that keeps the internet running smoothly.
When you type a URL into your browser and hit enter, your browser initiates an HTTP
request to the corresponding web server. This request consists of several components. Let's
dive into the nuts and bolts of an HTTP request, breaking it down into its different parts.
HTTP Methods
There are different methods for different actions you want to take. For example, you might
use GET when you're fetching information, POST when you're sending data to be
processed, PUT when you're updating something, or DELETE when you're removing a
resource.
URL
This is simply the address of the web resource you're trying to access. It's like telling the
courier where to go.
Headers
These are like little notes attached to your request, providing extra information. For instance,
the user-agent header tells the server what kind of browser or device you're using, and the
content-type header specifies the format of the data you're sending or expecting to receive.
Now, if you're sending data to the server, like when you're submitting a form, you'll have a
body in your request. This is where the actual data goes, often in formats like JSON or form-
encoded data.
HTTP requests serve as the communication bridge between clients (such as web browsers
or mobile apps) and servers, enabling the retrieval and manipulation of resources.
Now, there are a few different types of these requests, each with its own job:
3/10
POST Request: Used to submit data to be processed to a specified resource.
A GET request is like asking a server for information without sending any additional data
along with the request. GET requests are primarily used to retrieve data from a server.
Imagine you're at a restaurant, and you're browsing through the menu, trying to decide what
to order. You don't have anything to give to the waiter yet, you're just looking at the options
available. In this scenario, you're making a GET request.
Let's say you want to see the latest news headlines on a news website. When you type the
website's URL into your browser and hit enter, your browser sends a GET request to the
server hosting the news website. The server then responds by sending back the webpage
containing the latest news headlines.
4/10
When someone sends a GET request to this route, Flask calls this function called 'get_book'.
Inside this function, we've got some logic to retrieve details about a book from a database.
We use the book's ID, which is provided in the URL, to fetch the right book details.
Once we've got those details, we package them up nicely into a dictionary called
'book_details'. This dictionary contains information like the book's title, author, and genre. In
the real world you’d want to use a database for this step.
Now, here comes the important part. We use Flask's 'jsonify' function to convert this
dictionary into JSON format. JSON is just a way of structuring data so that it's easy for
computers to read and understand.
Handling Responses
A crucial aspect of working with HTTP requests is understanding server responses. This is a
key part of the process because it's how you know what's going on with your requests.
These little numbers tell you a lot about what happened with your request. For example, if
you get a 200, that means everything went smoothly. But if you get something like a 3xx, it
means you're being redirected somewhere.
JSON is a popular format for sending data back and forth between servers and clients. So,
when you send a request and get JSON back, you need to know how to unpack it.
Because let's face it, nobody wants a buggy app. So, knowing how to manage those
responses is key and you will get really good at this if you start making Flask apps.
Imagine you're at a post office, and you want to send a package to a friend. You write a letter,
put it in an envelope, address it, and hand it over to the post office staff. In this scenario,
you're making a POST request.
In the context of web development, a POST request is used to submit data to be processed
by a server. It's like filling out a form on a website and clicking the submit button. POST
requests typically include a body containing the data to be processed by the server.
This is only a chunk of what the full article contains, with less jokes and analogies. If you
want to learn more to further your understanding, check it out here.
5/10
This request contains all the information you entered in the form.
Now, when someone sends a POST request to '/books', Flask kicks off this function called
'create_book'. Inside this function, the first thing we do is grab the JSON data that's been
sent along with the request.
Once we've got that data, we can use it to create a new book entry in the database. Now, in
this example, we're keeping things simple, so we're just generating a dummy book ID, which
is 6969. But in a real-world scenario, you'd have some logic here to actually insert the new
book into the database.
After creating the book, we put together a response message to send back to the client. This
response message confirms that the book was created successfully, and it includes the ID of
the newly created book.
In Project X we together built an interactive CRUD Flask Web Application. Throughout this
entire project I taught you guys how we use certain HTTP requests to move data around our
application.
6/10
Let’s take a look at the index/homepage of our application where here I used POST and GET
to send off data to our SQL Alchemy database.
When we send a request the first thing our function is doing is checking if it is a POST
method. If it’s a POST, then we want to collect the information within our HTML form.
The data we collect for the HTML form is most likely done through the input element that is
stored within the HTML form element. We can link which type of method in there.
❌
If you guys are here reading this far then you are a premium member of The Nerd Nook. This
means you have full access to the Flask Project we did for Project #2 in our Project
series. Head over and check it out if you missed it!
This is only a chunk of what the full article contains, with less jokes and analogies. If you
want to learn more to further your understanding, check it out here.
7/10
A PUT request is used to update existing data on the server. It's like editing a document and
saving the changes. PUT requests typically include a body containing the updated data.
For instance, suppose you have a to-do list application, and you want to mark a task as
completed. You send a PUT request to the server, indicating the task ID and its new status
(completed). The server then updates the task accordingly.
Unlock my full content to view all the interactive code examples here.
We've set up a route at '/books/int:book_id'. This route is specifically designed to handle PUT
requests. Think of a PUT request as a way to update or modify existing data on the server,
kind of like editing a document.
Now, when someone sends a PUT request to '/books/int:book_id', Flask calls this function
named 'update_book'. Inside this function, the first thing we do is grab the JSON data that's
been sent along with the request.
After performing the updates, we put together a response message to send back to the
client. This response message confirms that the book was updated successfully, and it
includes the ID of the book that was updated.
8/10
A DELETE request is used to remove existing data from the server. It's like deleting a file or
removing an item from a list. DELETE requests typically target a specific resource identified
by a URL.
This is only a chunk of what the full article contains, with less jokes and analogies. If you
want to learn more to further your understanding, check it out here.
For example, suppose you have a file storage application, and you want to delete a file from
your storage. You send a DELETE request to the server, specifying the URL of the file you
want to delete. The server then removes the file from the storage.
This route is specifically set up to handle DELETE requests. Think of a DELETE request as a
way to remove something, like deleting a file or erasing a line from your to-do list.
Now, when someone sends a DELETE request to '/books/int:book_id', Flask calls this
function named 'delete_book'. We're assuming here that the book ID exists in our database.
After deleting the book, we put together a response message to send back to the client. This
response message confirms that the book was deleted successfully, and it includes the ID of
the book that was deleted. It's as simple as that!
Conclusion
9/10
As we've explored the world of HTTP requests, we've really dug deep into the basics,
breaking down each part to understand how it fits into the big picture of web interactions.
We've looked at everything from the different HTTP methods that drive actions to the
structure of requests and responses, using Python and Flask to guide us along the way.
So, as you dive deeper into web development, remember that HTTP requests aren't just
lines of code – they're the bridges that connect users and servers, making the internet flow
smoothly. Embrace their language, and let your web creations thrive in the ever-changing
digital landscape.
FAQs
Check out the full article for my complete list of FAQ’s. All my full articles contain complete
breakdowns along with jokes and analogies to further your understanding.
The Nerd Nook is a reader-supported publication. To receive new posts and support my
work, consider becoming a free or paid subscriber.
Thank you for taking the time to read this week’s article!
👉 If you enjoyed this article, please leave it a ❤️. This helps more people discover this
button is located right below here! ⤵️
newsletter on Substack, which helps me out and shows me you enjoy content like this! The
10/10