An HTTP method is a verb that is used on a resource. There are four main HTTP methods: GET, POST, PUT, DELETE which should be used in certain situations. For example:
- GET is used to download cacheable data from the server. It doesn’t have a request body. It can have side effects, but it’s unexpected. GET can be conditional (If-Modified-Since) or partial.
- POST is used to submitting form data. Has a request body, and request that the resource do something with the enclosed entity. Although typically POST is used to create data, it can also be used to update resources on the server.
- PUT is commonly used for updating resources on the server. Has a request body. If the resource exists, it will be updated otherwise it will be created.
- DELETE is used for deleting files from a server. It doesn’t have a request body.
Besides of these, there are other HTTP methods:
- HEAD is used to fetch meta-data or data header from the entity.
- OPTIONS gives you back the possible operations that you can use over a resource.
- PATCH is used to update or patch a set of fields in an entity.
The main difference between PUT and PATCH is that when using PUT you send the whole entity to the request object, while with PATCH there is no need to sent the whole entity, but only a set of fields in the entity.
Example on how to map your API endpoints based on HTTP methods
- GET is used to retrieve all the accounts
- POST is used to create a new account
- GET is used to retrieve the specified account
- PUT is used to update the specified account
- DELETE is used to delete the specified account
HTTP headers are key-value pairs that are sent with requests and responses
The http body is the message’s actual data.
An http response is a message that an http server sends to a client in response to a request
HTTP Status Code
HTTP status codes convey to the client the result of the server’s attempt to satisfy the request. Sometimes they are also response codes. An HTTP status code is a 3-digit number that shows the result of the request, they are raging from 100s to 500s.
The 1xx codes are used for informational purpose.
The 2xx are used for a successful response, where everything goes as expected. For example
- 200 OK
- 201 CREATED
The 3xx responses code are used for redirecting proposes (when a resource was moved to another part of the network. In this case, the response body should contains the indication regarding where the resource was moved.
The 4xx level codes are used to indicate errors clients level code.
The 5xx level code are used to indicate server related problems. For example the 500 response code indicates that the server is not able to respond to the request.
In order to recap:
- 200 OK. is used for successful requests.
- 201 CREATED. returns a location header for the new resource.
- 202 ACCEPTED. server has accepted the request, but it is not yet complete.
- 301 it is used when a resource was moved to a new location (this status represents permanently moved).
- 400 – BAD REQUEST. Used in case of malformed syntax. Retry with changes.
- 401 – UNAUTHORIZED. Used when authentication is required.
- 403 – FORBIDDEN. Used when server refuses the request (even it has understood it).
- 404 – NOT FOUND. It is used when a server resource could not be found.
- 406 – INCOMPATIBLE. It is used in case of incompatible Accept Header.
- 409 – CONFLICT. It is used when resource conflicts with client request.
- 500 – INTERNAL SERVER ERROR. This status indicates a server error. it is sent when something wronghappens on the server.