# HTTP/HTTPS Method & Status Code

## What is HTTP Methods ?  

HTTP is the application layer protocol used for communications. The method indicate the intended actions or semantics of a particular HTTP request.

HTTP methods is the request method that indicates the intended actions or a semantics a client want a server to perform on resources.

HTTP methods specify the intended operations of an HTTP request sent by client to a server. Different methods such as, `GET, POST, PUT, PATCH and DELETE`, have different semantics for retrieving or modifying resources. In Restful APIs, these methods are commonly used for CRUD operation on resources.

Imagine you have a e-book platform. Your database store the books pdf and your backend expose API endpoint to interact with it.  
The URL identifies the resources you want to interact with, while the HTTP method indicates the intended operations.

For Example : `/api/e-book/book/Courage-to-be-Disliked`

`GET` : Retrieve a resource ( We indicate get request on this URL to fetch the book)

`POST` : Creating or Submit the Data (This method can use to create any rating or comment)

`PUT` : Replacing or Modifying any Book Information (Here we have to give full data like POST request for modify).

`PATCH` : Modifying Partially Information of Book (In this method We give only info which we have to modify).

`DELETE` : To Remove Information or Book (this method indicate that the client want to remove their information).

`HEAD` : TO fetch Headers (this method only fetch the headers)

`OPTIONS` : to check the capability or use in CORS

These are common and most used method in Backend.

### Let's Talk about idempotency of methods

| HTTP Method | Idempotent? | Reason |
| --- | --- | --- |
| `GET` | Yes | Retrieves data without requesting a change to resource state. |
| `POST` | No, generally | Repeated requests can create duplicate resources or repeat an action. |
| `PUT` | Yes | Replaces the resource representation with the specified representation. |
| `PATCH` | Not always | Depends on the operation being performed. |
| `DELETE` | Yes | Repeated requests have the same intended effect on resource state. |

## MOST Common Status Code used in Response

| Status Code | Name | Meaning |
| --- | --- | --- |
| `200` | OK | Request succeeded. |
| `201` | Created | Resource created successfully. |
| `204` | No Content | Request succeeded; no response body. |
| `400` | Bad Request | Invalid or malformed request. |
| `401` | Unauthorized | Authentication is missing or invalid. |
| `403` | Forbidden | Access to the resource is denied. |
| `404` | Not Found | Requested resource does not exist or cannot be found. |
| `409` | Conflict | Request conflicts with the current resource state. |
| `422` | Unprocessable Content | Request content cannot be processed as submitted. |
| `429` | Too Many Requests | Too many requests sent within a given period. |
| `500` | Internal Server Error | Unexpected server-side error. |
| `502` | Bad Gateway | Gateway received an invalid upstream response. |
| `503` | Service Unavailable | Server is temporarily unavailable. |
| `504` | Gateway Timeout | Gateway timed out waiting for an upstream response. |

### Quick way to remember

*   2xx: Success
    
*   4xx: Problem with the request or the client's authorization
    
*   5xx: Server-side failure
