A Beginner's Guide to HTTP

A Beginner's Guide to HTTP

HTTP stands for Hypertext Transfer Protocol. It is an application layer protocol in the TCP/IP layer that acts as a gateway to the internet. It is mainly a client-side protocol that helps access information from the internet.

HTTP Methods

Major HTTP Methods with Explanations:

  1. GET:

    • Purpose: Retrieves data from the server.

    • Example: When you visit a webpage, your browser sends a GET request to fetch the page's content.

  2. POST:

    • Purpose: Sends data to the server to create a new resource or trigger an action (e.g., form submission, file upload).

    • Example: Submitting a registration form.

  3. PUT:

    • Purpose: Updates or replaces an existing resource on the server with the provided data.

    • Example: Updating a user's profile information.

  4. DELETE:

    • Purpose: Removes a specified resource from the server.

    • Example: Deleting a post or user from a website.

  5. PATCH:

    • Purpose: Partially updates a resource, applying only the changes specified in the request.

    • Example: Modifying just one field of a user's profile, such as changing their email address.

    • Not Idempotent: Repeated requests can modify the resource further.

  6. HEAD:

    • Purpose: Similar to GET, but it only retrieves the headers of a resource, not the body.

    • Example: Checking metadata (e.g., last modified date) of a file without downloading it.

  7. OPTIONS:

    • Purpose: Describes the communication options for the target resource, such as which HTTP methods are supported.

    • Example: Checking if a server supports PUT or DELETE requests.

Status Codes

HTTP Status Codes:

HTTP status codes are three-digit numbers sent by the server to indicate the result of a client's request. They are grouped into five categories:

1. 1xx - Informational:

  • 100 Continue: The server has received the request and the client can continue sending the request body.

  • 101 Switching Protocols: The server is changing protocols as requested by the client (e.g., switching to WebSocket).

2. 2xx - Success:

  • 200 OK: The request was successful and the server has returned the requested data.

  • 201 Created: A new resource has been successfully created (e.g., after a POST request).

  • 204 No Content: The request was successful, but there’s no content to return (e.g., after a DELETE request).

3. 3xx - Redirection:

  • 301 Moved Permanently: The resource has been permanently moved to a new location.

  • 302 Found: The resource is temporarily located elsewhere (commonly used in redirects).

  • 304 Not Modified: The resource hasn’t been modified since the last request (used for caching).

4. 4xx - Client Errors:

  • 400 Bad Request: The server couldn't understand the request due to invalid syntax.

  • 401 Unauthorized: The request requires authentication, and the client hasn’t provided valid credentials.

  • 403 Forbidden: The server understands the request but refuses to authorize it.

  • 404 Not Found: The requested resource could not be found on the server.

  • 405 Method Not Allowed: The HTTP method used is not supported by the resource.

5. 5xx - Server Errors:

  • 500 Internal Server Error: The server encountered an error it couldn't process.

  • 502 Bad Gateway: The server received an invalid response from an upstream server.

  • 503 Service Unavailable: The server is currently unavailable (overloaded or down for maintenance).

  • 504 Gateway Timeout: The server did not receive a timely response from an upstream server.

These status codes provide essential information about the result of HTTP requests, helping clients understand whether a request was successful, failed, or needs to be redirected.