Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

HTTP Fundamentals Review

1. Introduction

The Hypertext Transfer Protocol (HTTP) is the foundation of data communication on the World Wide Web. It is an application-layer protocol that facilitates the transfer of data between clients and servers. Understanding HTTP is crucial for web developers, network engineers, and anyone involved in web technology.

2. HTTP Methods

HTTP defines several request methods, also known as HTTP verbs, that indicate the desired action to be performed on the specified resource. The most common HTTP methods include:

  • GET: Requests data from a specified resource.
  • POST: Submits data to be processed to a specified resource.
  • PUT: Updates a specified resource with provided data.
  • DELETE: Removes the specified resource.
  • HEAD: Similar to GET but only retrieves the headers without the body.

Here’s a simple example of a GET request using JavaScript's Fetch API:


fetch('https://api.example.com/data')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));
                

3. HTTP Status Codes

HTTP responses include status codes that indicate the outcome of the request. Status codes are grouped into five categories:

  • 1xx: Informational - Request received, continuing process.
  • 2xx: Success - The action was successfully received, understood, and accepted.
  • 3xx: Redirection - Further action needs to be taken in order to complete the request.
  • 4xx: Client Error - The request contains bad syntax or cannot be fulfilled.
  • 5xx: Server Error - The server failed to fulfill a valid request.

Example of a 404 Not Found response:


HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 1234


404 Not Found

404 Not Found