HTTP Methods: A Complete Guide to 5 Important Types

Hey there, fellow coders! Today, we’re going to dive into the world of HTTP methods, the backbone of web development. You’ll learn about the primary HTTP methods, their use cases, and how to use them with curl. Buckle up, and let’s get started!

What is HTTP(Hypertext Transfer Protocol)?

Have you ever wondered how your web browser magically retrieves that hilarious cat video or remembers your shopping cart items? The secret lies in a hidden language called HTTP, the communication backbone of the web. Just like you use words to ask a librarian for a book (tell them what you want!), HTTP methods act as instructions sent to servers, telling them what action to take with your request. Understanding these methods empowers you to navigate the web more effectively, even if you’re not a tech whiz!

This article will cover the most commonly used HTTP methods—GET, POST, PUT, PATCH, and DELETE using the API available at jsonplaceholder as a practical example. We’ll also discuss how to use cURL to make HTTP requests.

http methods

About cURL: Your Pocket-Sized Web Communicator

Think of curl as your personal interpreter for this web language. It’s a free and powerful command-line tool that allows you to directly interact with websites by sending HTTP requests. Installing curl is usually straightforward and depends on your operating system.

Importance of cURL

  • Cross-Platform Support: cURL runs on various operating systems, making it a versatile choice for developers.
  • Simplicity: It allows developers to send requests quickly through the command line or scripts.
  • Rich Features: It supports various options such as sending headers, handling cookies, using proxies, and including authentication.

Installation Process

To install cURL, follow the instructions based on your operating system:

  • Windows:
    1. Get the cURL executable from the official cURL website.
    2. Add the path to the cURL directory to your system’s PATH environment variable.
  • Linux:
    1. Most Linux distributions now include the curl package by default. You can verify by opening a terminal and typing curl --version.
    2. If curl is not already installed, you can install it using your distribution’s package manager. Example, on Ubuntu: sudo apt install curl
  • macOS:
    1. cURL is usually pre-installed on macOS.You can verify by opening a terminal and typing curl -v.
    2. If not, you can install it using Homebrew: brew install curl

Once installed, you can start using cURL to make HTTP requests.

Detailed Explanation of Each Method

1. GET: The Information Seeker

Think of asking a librarian for a book. The GET method retrieves data from websites, servers, or APIs. It’s perfect for fetching public information from APIs, like grabbing all users or a random user profile with specific details from jsonplaceholder API.

http-get-example
Request: bash curl -X GET 'https://jsonplaceholder.typicode.com/users' or curl -X GET 'https://jsonplaceholder.typicode.com/users/1' Response: json { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" }

2. POST: The Data Deliverer

Ever filled out a form on a website and clicked “submit”? That’s the POST method in action! It sends information to the server, typically used for creating new accounts, submitting data, or placing orders. We don’t expect to get the same result every time we send a POST request. The jsonplaceholder API allows creating users in the below way.

http-post-example
Request: bash curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe","email":"john.doe@example.com"}' https://jsonplaceholder.typicode.com/userscom/users Response: json { "name": "John Doe", "email": "john.doe@example.com", "id": 11 }

3. PUT: The Resource Revamp

Imagine completely renovating your room. The PUT method updates existing data on the server. It’s like replacing the entire information for a specific resource, such as updating your email address in an online profile. The jsonplaceholder API uses PUT to allow you to edit your profile information.

http-put-example
Request: bash curl -X PUT -H "Content-Type: application/json" -d '{"phone":"1234567890","username":"Leanne","email": "new.email@exmp.com"}' https://jsonplaceholder.typicode.com/users/1 Response: json { "phone": "1234567890", "username": "Leanne", "email": "new.email@exmp.com" "id": 1 }

4. PATCH: The Partial Fixer

Imagine fixing a small hole in your wall. The PATCH method is used for partial updates to a resource, allowing you to modify specific parts of the data. The jsonplaceholder API uses PATCH to allow you to update only your phone number without changing other details.

http-patch-example
Request: bash curl -X PATCH -H "Content-Type: application/json" -d '{"name":"Sally"}' https://jsonplaceholder.typicode.com/users/1 Response: json { "name":"Sally", "id": 1 }

5. DELETE: The Resource Remover

Imagine throwing away an old newspaper. The DELETE method deletes a resource from the server. Think about deleting an inactive user.

http-delete-example
Request: bash curl -X DELETE https://jsonplaceholder.typicode.com/users/1 Response: json { "success": true, "message": "User deleted successfully" }

Differences Between Methods

Here are the key differences between each HTTP method:

  • GET vs. POST: GET is used for retrieving resources, while POST is used for creating new resources.
  • PUT vs. PATCH: PUT is used for updating existing resources, while PATCH is used for partially updating existing resources.
  • DELETE vs. PUT: DELETE is used for deleting resources, while PUT is used for updating existing resources.

Conclusion

Understanding HTTP methods is crucial for web developers and anyone working with APIs. Each method has a specific purpose—whether retrieving data with GET, sending new data with POST, or updating resources with PUT and PATCH. Learning how and when to use these methods can make your interactions with APIs much more efficient and error-free.

By using cURL and a simple API like Random User, you can test and experiment with these HTTP methods in real-time. Mastering these concepts will not only improve your API integration skills but also deepen your understanding of web communication.

So, the next time you’re working with an API, keep these HTTP methods at your fingertips. With this knowledge, you’ll be able to navigate and manipulate data efficiently, turning you into a more capable and versatile developer!