Introduction

In this article, we'll see an explication of the most used HTTP methods such as GET, POST, PUT, PATCH, and DELETE, making an analogy with some spells from the Harry Potter universe.

Content

  1. Accio
  2. Aguamenti
  3. Vera Verto
  4. Reparo
  5. Reducto
  6. Conclusion

Accio

"The Accio spell is used to summon long-distance items into the witcher's hands. To use it, one must point the wand at the object and say Accio + Name of the chosen object".

Harry using the Accio spell to pull the remote control

In HTTP, the Accio is equivalent to the GET method. We use it to get a representation of something. For example, to get the data of a book in the Hogwarts Library, we can make the following call in the system:

REQUEST
METHOD: GET
URL: localhost:8080/books/42

RESPONSE
{
    "id": 42,
    "name": "Quidditch Through the Ages",
    "author": "Kennilworthy Whisp"
}
Enter fullscreen mode Exit fullscreen mode

A power we have with the GET method, but we don't have on the Accio spell, is the pull more of one item directly, as is the case of using the GET without passing the book ID. Thus, per convention, understanding that we want to get all the books of the library:

REQUEST
METHOD: GET
URL: localhost:8080/books

RESPONSE
[
    {
        "id": 42,
        "name": "Quidditch Through the Ages",
        "author": "Kennilworthy Whisp"
    },

    {
        "id": 66,
        "name": "Fantastic Beasts and Where to Find Them",
        "author": "Newt Scamander"
    },

    ... More books ...
]
Enter fullscreen mode Exit fullscreen mode

Aguamenti

"The Aguamenti spell conjures pure water from witch wand. Depending on the concentration of the launcher, this spell can be a simple water jet until a wave".

Harry using Aguamenti spell to put water into a recipient

In HTTP, the Aguamenti spell is equivalent to the POST method that we can use to create anything. In our example, we have an endpoint that represents an empty jar:

REQUEST
METHOD: GET
URL: localhost:8080/jar/567/content

RESPONSE
{}
Enter fullscreen mode Exit fullscreen mode

To put water into this jar, we need to create it with all attributes and do a POST to the same URL (endpoint):

REQUEST
METHOD: POST 
CONTENT: {"quantity":"500ml", "type":"Water"} 
URL: localhost:8080/jar/567/content
Enter fullscreen mode Exit fullscreen mode

Calling the GET again, we see that the content of the jar now is filled:

REQUEST
METHOD: GET 
URL: localhost:8080/jar/567/content

RESPONSE
{
    "id": 1,
    "quantity": "500ml",
    "type": "Water"
}
Enter fullscreen mode Exit fullscreen mode

Vera Verto

"A Latin phrase that can be interpreted how 'I really change'... It is a spell that can transforms animals into goblets. This is a classic and highly refined type of transformation. The most commonly used animals for this spell are birds, cats, and mice".

Minerva using the Vera Verto spell to transform a bird into a goblet

In HTTP, when we want a complete change to the resource values, we can use the PUT method. For example, given a bird with properties like color, height, weight, and surface, we can transform it into a goblet, with has the same attributes but with different values.

Let's use the GET to take a look at the attributes of the bird:

REQUEST
METHOD: GET
URL: localhost:8080/teachers/minerva/materials/47

RESPONSE
{
    "id": 47,
    "color": "Black and White",
    "height": "63cm",
    "weight": "2kg",
    "surface": "Feathers"
}
Enter fullscreen mode Exit fullscreen mode

And using the PUT, we do the transformation by changing all the attributes:

REQUEST
METHOD: PUT
CONTENT: {"color": "Transparent", "height": "30cm", 
"weight", "85g", "surface": "Glass"}
URL: localhost:8080/teachers/minerva/materials/47
Enter fullscreen mode Exit fullscreen mode

When we call the GET again, we see that the resource of ID-47 is totally different:

REQUEST
METHOD: GET
URL: localhost:8080/teachers/minerva/materials/47

RESPONSE
{
    "id": 47,
    "color": "Transparent",
    "height": "30cm",
    "weight": "85g",
    "surface": "Glass"
}
Enter fullscreen mode Exit fullscreen mode

PS: This example is totally feasible in practice, but in objected-oriented programming, if your code allows turning a bird into a goblet, very likely you abstracted more than necessary.

Reparo

"The Reparo spell will repair broken objects with a flick of the wand. Accidents happen, so it's essential to know how to fix our mistakes".

Hermione using the Oculus Reparo spell to fix Harry's glasses

Unlike the PUT method, which totally updates a resource, the PATCH method updates a resource partially, changing one or more attributes separately.

Given a list of glasses, we can use the GET to check the status of Harry's glasses:

REQUEST
METHOD: GET 
URL: localhost:8080/students/harry-potter/glasses/123

RESPONSE
{
    "id": 123,
    "owner": "Harry Potter",
    "color": "Black",
    "frame": "Rounded",
    "status": "Broken"
}
Enter fullscreen mode Exit fullscreen mode

And next, the PATCH for performing the repair:

REQUEST
METHOD: PATCH 
CONTENT: {"status":"Fixed"} 
URL: locahost:8080/students/harry-potter/glasses/123
Enter fullscreen mode Exit fullscreen mode

When we call the GET again, we see that attribute "status" was alter from "Broken" to "Fixed":

REQUEST
METHOD: GET
URL: localhost:8080/students/harry-potter/glasses/123

RESPONSE
{
    "id": 123,
    "owner": "Harry Potter",
    "color": "Black",
    "frame": "Rounded",
    "status": "Fixed"
}
Enter fullscreen mode Exit fullscreen mode

Reducto

"With this powerful curse, skilled witches can easily reduce the obstacles into pieces. So, for obvious reasons, great care must be exercised when learning and practicing this spell".

Gina using the Reducto spell to destroy an obstacle

Simple to perform but requires special care. The DELETE method, on HTTP, removes a resource, usually using its ID. In this example, we'll use the DELETE to destroy a Horcrux, although the Reducto spell doesn't have enough power to do so.

First, we use the GET method to see details of Horcrux:

REQUEST
METHOD: GET
URL: localhost:8080/horcruxes/8

RESPONSE
{
    "id": 8,
    "type": "Nokia",
    "local": "Brazil"
}
Enter fullscreen mode Exit fullscreen mode

To destroy (delete) it, just call the same URL with the DELETE method:

REQUEST
METHOD: DELETE
URL: localhost:8080/horcruxes/8
Enter fullscreen mode Exit fullscreen mode

If we call the GET method again, nothing is returned, so the Horcrux was destroyed!

REQUEST
METHOD: GET
URL: localhost:8080/horcruxes/8

RESPONSE
{}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we learned how HTTP methods are used. We used GET to search for a book in the Hogwarts Library; POST to create water and fill a jar; PUT to turn a bird into a goblet; PATCH to fix Harry's glasses and DELETE to destroy a Horcrux.

These methods are the most important and widely used in WEB applications like REST APIs and CRUD systems (Create, Read, Update and Delete). There are still other methods like Head, Connect, Options, and Trace that weren't explained here, but who knows in the next post?

See you next time!
Mischief managed...

References

Fandom - https://harrypotter.fandom.com/pt-br/wiki/
Alura - https://www.alura.com.br/curso-online-http-fundamentos
Mozilla - https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

PS: Special thanks to Gabriella Silva, Marivaldo Sena and Guilherme Aleixo for helping to review parts of this text.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐