Python + Flask - Part 2 - HTTP Methods
Tags: python, flask
Published: 2019-10-20 02:50:20

![titulo](https://thepracticaldev.s3.amazonaws.com/i/f0i5oszdj3gwk686xuc0.JPG)

This is the second part of a series about the Flask framework, a common tool used to create web applications with Python.
## Objectives
The part 2 will focus on creating HTTP methods to our web API.
The full example is available here: [Python-Flask](https://github.com/lucianopereira86/Python-Flask).
## Topics
- [GET](#get)
- [POST](#post)
- [PUT](#put)
- [DELETE](#delete)

### GET

Create a dictionary called _users_dict_ to be returned after a GET request:

![flask04](https://thepracticaldev.s3.amazonaws.com/i/cubm4u9eh75amitudbpo.JPG)

Access the URL below and see the result:

```
http://127.0.0.1:5000/users
```

![flask05](https://thepracticaldev.s3.amazonaws.com/i/sm5t5k3c7m1d7c0vjffs.JPG)

Create another method to return a user by id:

![flask06](https://thepracticaldev.s3.amazonaws.com/i/l2oari7j0c6eklwdnyyh.JPG)

Access the URL below and see the result:

```
http://127.0.0.1:5000/user?id=1
```

![flask07](https://thepracticaldev.s3.amazonaws.com/i/p04q6ph1iplvt1e8dkty.JPG)

Return a user by id passing the parameter in the path:

![flask08](https://thepracticaldev.s3.amazonaws.com/i/087p2dlzzmnp53x1mj1s.JPG)

Access the URL below and see the result:

```
http://127.0.0.1:5000/user/1
```

![flask09](https://thepracticaldev.s3.amazonaws.com/i/ewz8ddip4vvo6r4vgozy.JPG)

### POST

To simulate a POST request, remove all dictionaries from _users_dict_:

![flask10](https://thepracticaldev.s3.amazonaws.com/i/oby6wmg7vlrlnzmqzn31.JPG)

Create a POST method with the following code:

![flask11](https://thepracticaldev.s3.amazonaws.com/i/x8g1udcj5cbdwczglrvz.JPG)

Execute a POST request sending a new user. You can use the [Postman](https://www.getpostman.com/) to make the request:

![flask12](https://thepracticaldev.s3.amazonaws.com/i/avbyi7y44y6seuxbo2wg.JPG)

The result will be:

![flask13](https://thepracticaldev.s3.amazonaws.com/i/92617o55niojne7anvfh.JPG)

A new user was added to the _users_dict_. Make some changes at the _get_users_ function to search for the user by using multiple properties dynamically, even by considering their types.

| Don't forget to run the POST request every time you save the project because the _users_dict_ will be resetted! |
| --------------------------------------------------------------------------------------------------------------- |


![flask14](https://thepracticaldev.s3.amazonaws.com/i/0hsz9aydog2cypc7lvca.JPG)

This way there will be many possible queries with the same method:

![flask15](https://thepracticaldev.s3.amazonaws.com/i/ewz5yp5zmpgs5qn341en.JPG)

![flask16](https://thepracticaldev.s3.amazonaws.com/i/fk02vg4tfxv0w8zyjqcr.JPG)

![flask17](https://thepracticaldev.s3.amazonaws.com/i/zy8zsnix1zrgzmn96luf.JPG)

### PUT

To update a user, create this method:

![flask18](https://thepracticaldev.s3.amazonaws.com/i/hii1bq5gzzeidymu3zh7.JPG)

Execute a PUT request:

![flask19](https://thepracticaldev.s3.amazonaws.com/i/5f3kdw8e0no3rsolj76x.JPG)

The result will be:

![flask20](https://thepracticaldev.s3.amazonaws.com/i/a8a1ua8lqpc6quz0mo3j.JPG)

### DELETE

To delete a user by id, write this code:

![flask21](https://thepracticaldev.s3.amazonaws.com/i/voi080f561v0v24z5ecl.JPG)

Execute a DELETE request:

![flask22](https://thepracticaldev.s3.amazonaws.com/i/dh0su5vgonl3d826z0v4.JPG)

The result will be:

![flask23](https://thepracticaldev.s3.amazonaws.com/i/erddiqcayul7lbvreab4.JPG)
## Next

We will connect our web API with a local database.