Verbs

The HTTP verbs are the magic behind RESTful APIs. Let's use an example to understand how HTTP verbs works. We want to create an API to manage products; our first version might look like this:

Endpoint HTTP verb Goal
http://myapp/api/createProduct POST To create a product
http://myapp/api/updateProduct/P1 POST To update an existing product
http://myapp/api/listProducts GET To retrieve the complete list of products
http://myapp/api/viewProductDetails/P1 GET To retrieve a single product
http://myapp/api/deleteProduct/P1 POST To delete a product

 

From the preceding table, you can note that we need to remember five endpoints, and we are using two HTTP verbs: POST and GET. It might be understood that every time we want to retrieve information, a GET verb is used, and to perform operations that will modify the existing information in our system, a POST verb is used.

So, let's make our endpoints easy to remember using HTTP verbs. After applying a simple refactor, now our table might look as follows:

Endpoint HTTP verb Goal
http://myapp/api/products

GET

POST

To retrieve the full list of products.

To create a new product.

http://myapp/api/products/P1

GET

DELETE

PUT

To retrieve a single product.

To delete an existing product.

To modify an existing product. In this case, you must send the full document, not just the fields that have changed.

 

Now, we are using the HTTP verbs, and we have reduced from five endpoints to just two. HTTP verbs are sent with every request to the server, so servers can use them to identify what operation a user wants to perform, sending the correct endpoint and verb.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset