DEV Community

Keith Capers
Keith Capers

Posted on

RESTful GET and POST Requests: A Beginners Guide

If you are building a web app chances are you will need to work with API's at some point. API's (Application Programming Interfaces) allow different systems to communicate with each other, and REST(Representational State Transfer) is one of the most popular design patterns.

Two of the most commonly used HTTP methods in an API are a GET and POST. In this guide, I will be breaking down to you how to use both of them in your Flask app.

What is a GET Request?

A GET request is the most common HTTP request method. It signifies that the client is attempting to view the located resource. For example when you open a website or search for something on Google or your favorite website your browser sends a GET request to the server for the info it needs to be displayed to the page. In your Flask app you will use a GET request to get the data from the database you create.

What is a POST Request?

A POST request is the second most common HTTP request method. It signifies that the client is attempting to submit a form to create a new resource. So that means when you click "Submit" on a contact form or add a comment on twitter, your browser is sending a POST request with the data you input into the server.

Now that we know all of that information we will take it slow and create a simple Book Library app.

Book Library

In the Book Library app we will be,

  1. Retrieving a list of books which will be the GET request.
  2. Adding a new book to the list of books which is our POST request.

After these steps the user of our app will be able to see a nice long list of books, like The Great Gatsby, Of Mice and Men and Lord of The Flies. And if they wanted to add another book like The Scarlet Letter the server adds the book to the database, and the user will see the newly added book in the list of books the next time they make a GET request.

Step 1 Creating the GET endpoint

To be able to fetch the list of books we will create a route that handles
GET requests.

class Books(Resource):

    def get(self):

        response_dict_list = [n.to_dict() for n in Book.query.all()]

        response = make_response(
            response_dict_list,
            200,
        )

        return response

api.add_resource(Books, '/books')
Enter fullscreen mode Exit fullscreen mode

Some key points to look at here are:
1) The method is specified at the top with def get.
2) response_dict_list is a variable that is querying the database to get all of the books from Book, in a list comprehension.
3) The use of to_dict() which helps turn each book object into dictionary format.
4) And lastly the route for our browser which is '/books'.

Step 2 Creating the POST request

To be able to allow users to add a new book to the list, we will create our POST request which we will keep at the same endpoint.

def post(self):
    new_book = Book(
        title=request.form['title'],
    )

    db.session.add(new_book)
    db.session.commit()

    response_dict = new_book.to_dict()

    response = make_response(
        response_dict,
        201,
    )

    return response
Enter fullscreen mode Exit fullscreen mode

As you can see again we define the method at the top. But what is different here is that we create a new_book variable that allows us to add a new title. After that we add the new_book and complete the transaction with the db.session.commit(). Again we want our data to be in dictionary format so we call .to_dict() on that new book before returning it for a response.

After those 2 steps are done all that's left would be to test the endpoints in your browser! Once you do that you will see that GET is the perfect way to retrieve data from a server and POST is the perfect way to create new data and send it to the server.

Conclusion

All in all together these two methods form the basis of a lot of your favorite apps since most use RESTful API's, they allow users to easily interact with there app. GET and POST requests may seem difficult to learn but once you break them down they really are not difficult to understand. Here I showed how to do 2 different HTTP request methods and while there are a few more like a PATCH request which is an HTTP request method that signifies that the client is attempting to update a resource with new information. And DELETE request an HTTP request method that signifies that the client is attempting to delete a resource. With just these 2 you are on your way to making some nice flask apps with a lot of the modern day functionality that you need.
_

Top comments (0)