In web development, communication between clients, such as browsers or API clients, and servers happens through HTTP requests. The methods used in these requests specify the type of action being performed. Each HTTP method instructs the server to perform a different operation, such as retrieving, adding, updating, or deleting data. Let's go through all HTTP methods one by one.
GET is used to retrieve data from the server. It does not make any changes on the server; it only fetches information. For example:
GET /api/customers
This request returns all customers from the server. It is read-only and does not change the data.
POST is used to add a new resource to the server. The submitted data is included in the request body. For example:
POST /api/customers
It is used to create a new customer record. It sends data to the server and usually returns a newly created resource.
PUT is used to completely update an existing resource. Although it looks similar to POST, the difference is that it overwrites the existing resource. For example:
PUT /api/customers/123
This request updates all information for the customer with ID 123.
PATCH is used to update only specific fields of a resource. Unlike PUT, it sends only the changed fields rather than the entire data. For example:
PATCH /api/customers/1234
{ "name": "foobar" }
This request updates only the customer's name and does not change the other fields.
DELETE is used to delete a resource. For example:
DELETE /api/customers/235
This request deletes the customer with ID 235. No matter how many times it is called, the result is the same: the resource is deleted.
HEAD works similarly to GET, but it retrieves only header information from the server and does not return body data. It is usually used to check whether a resource exists or to inspect its metadata. For example:
HEAD /api/customers
OPTIONS queries which HTTP methods are supported for a URL. It is especially used in CORS (Cross-Origin Resource Sharing) operations. For example:
OPTIONS /api/main.html/1.1
This request returns which HTTP methods can be used at the specified address.
TRACE is used to see what happens to a request after it reaches the server. It is preferred for diagnostic and debugging purposes. For example:
TRACE /api/main.html
The server returns the client's request exactly as it received it.
CONNECT is used to establish a tunnel connection between the client and the server. It is generally involved in creating secure connections such as HTTPS. For example:
CONNECT www.example.com:433 HTTP/1.1
This request creates a two-way secure channel between the client and the server.
Summary Table
| Method | Purpose | Data Sent Through | Changes Data? |
|---|---|---|---|
| GET | Retrieve data | URL | No |
| POST | Add new data | Body | Yes |
| PUT | Completely update data | Body | Yes |
| PATCH | Partially update data | Body | Yes |
| DELETE | Delete data | URL | Yes |
| HEAD | Check without data | URL | No |
| OPTIONS | See supported methods | URL | No |
| TRACE | Diagnostics | URL | No |
| CONNECT | Establish a secure connection | URL | Yes |
HTTP methods form the backbone of web applications. When designing RESTful APIs, using these methods correctly helps make the system reliable, readable, and maintainable. Using each method according to its purpose keeps communication clear on both the client and server sides.