Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

HTTP Basics

What is HTTP?

HTTP (HyperText Transfer Protocol) is the foundation of any data exchange on the Web. It is a protocol used for transmitting hypertext requests and information on the internet. HTTP follows a request-response protocol in the client-server computing model.

HTTP Methods

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource:

  • GET: Requests a representation of the specified resource. Requests using GET should only retrieve data.
  • POST: Submits data to be processed to a specified resource.
  • PUT: Updates a current resource with new data.
  • DELETE: Deletes the specified resource.
  • HEAD: Asks for a response identical to that of a GET request, but without the response body.
  • OPTIONS: Used to describe the communication options for the target resource.
  • PATCH: Applies partial modifications to a resource.

HTTP Status Codes

HTTP responses include status codes to indicate the success or failure of the request. They are grouped into five classes:

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

Example: Basic HTTP Request and Response

Let's see a simple example of an HTTP GET request and the corresponding response.

HTTP GET Request

GET /index.html HTTP/1.1
Host: www.example.com
Connection: close

HTTP Response

HTTP/1.1 200 OK
Date: Mon, 27 Jun 2024 12:28:53 GMT
Server: Apache/2.4.1 (Unix)
Last-Modified: Tue, 15 Nov 2023 12:45:26 GMT
Content-Length: 362
Content-Type: text/html
Connection: Closed




    Example Page


    

Hello, World!

This is a simple HTML document.