Lesson 9 of 158 – HTTP Protocol
9%

HTTP Protocol

HTTP stands for HyperText Transfer Protocol. It is a communication protocol commonly used for transferring information between a client and a server.

REST APIs commonly use HTTP to allow applications such as React Native mobile apps to communicate with backend servers.

Note: In this course, React Native will send HTTP requests to our PHP REST API, and the PHP server will return HTTP responses.

1. What is HTTP?

HTTP stands for HyperText Transfer Protocol.

HTTP defines how messages are exchanged between clients and servers over a network.


Client
  ↓
HTTP Request
  ↓
Server
  ↓
HTTP Response
  ↓
Client

2. Why is HTTP Important?

HTTP is important for REST API development because it provides the communication mechanism between the client and server.

A React Native application can use HTTP requests to communicate with a PHP REST API.

3. HTTP Client and Server

HTTP communication normally involves a client and a server.


React Native
     ↓
HTTP Request
     ↓
PHP REST API
     ↓
HTTP Response
     ↓
React Native

The client starts the communication by sending a request.

4. HTTP Request

An HTTP request is a message sent from the client to the server.

For example:


GET /api/students

This request can ask the server to return student information.

5. HTTP Response

An HTTP response is a message sent from the server back to the client.

For example:


HTTP/1.1 200 OK

{
    "success": true,
    "message": "Students found"
}

6. HTTP Request Structure

An HTTP request can contain several important components.

  • Request method
  • Request URL
  • Request headers
  • Query parameters
  • Request body

The exact parts used depend on the request.

7. HTTP Response Structure

An HTTP response can contain:

  • Status code
  • Response headers
  • Response body

Status Code
     ↓
Headers
     ↓
Response Body

8. HTTP Methods

HTTP provides methods that describe the intended operation of a request.

Method Common Use
GET Retrieve data
POST Submit or create data
PUT Update a resource
PATCH Partially update a resource
DELETE Delete a resource

9. GET Request

GET is commonly used when the client wants to retrieve information.


GET /api/students

The server can process the request and return a list of students.

10. POST Request

POST is commonly used to send data to the server.


POST /api/students

The request can contain student information in the request body.

11. PUT Request

PUT is commonly used to update an existing resource.


PUT /api/students/10

The server can use the ID to identify the resource that needs to be updated.

12. PATCH Request

PATCH is commonly used to partially update an existing resource.


PATCH /api/students/10

For example, only the student's email address may need to be changed.

13. DELETE Request

DELETE is commonly used to remove a resource.


DELETE /api/students/10

The server can process the request and remove the corresponding record.

14. HTTP URL

A URL identifies the location of a resource or API endpoint.

Example:


https://example.com/api/students

The client sends its HTTP request to this URL.

15. HTTP and HTTPS

HTTP and HTTPS are both used for web communication.

HTTPS is HTTP communication protected using encryption through TLS.

Protocol Example
HTTP http://example.com
HTTPS https://example.com

For production applications, HTTPS should be used to protect data during network communication.

16. HTTP Headers

HTTP headers provide additional information about a request or response.

Example request header:


Content-Type: application/json

Headers can also carry authentication information.

17. Content-Type Header

The Content-Type header tells the server what type of data is being sent in the request body.

For JSON:


Content-Type: application/json

This is commonly used when React Native sends JSON data to a REST API.

18. Accept Header

The Accept header can indicate the type of response the client expects from the server.

Example:


Accept: application/json

This tells the server that the client expects JSON data.

19. HTTP Status Codes

HTTP status codes tell the client what happened while processing the request.

  • 2xx – Successful requests
  • 3xx – Redirection
  • 4xx – Client-side errors
  • 5xx – Server-side errors

20. Common HTTP Status Codes

Status Meaning
200 OK Request succeeded
201 Created Resource was created
400 Bad Request Request contains invalid data
401 Unauthorized Authentication is required or invalid
404 Not Found Requested resource was not found
500 Internal Server Error Server encountered an error

21. HTTP Request with JSON

A POST request can send JSON data to a REST API.


POST /api/students
Content-Type: application/json

{
    "name": "Rahul",
    "email": "rahul@example.com",
    "course": "React Native"
}

The PHP API can read this JSON and process the data.

22. HTTP Response with JSON

The server can return JSON in the response body.


HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "message": "Student found",
    "data": {
        "id": 10,
        "name": "Rahul"
    }
}

23. HTTP Request in React Native

React Native can send an HTTP request using the Fetch API.


fetch("https://example.com/api/students")
    .then(response => response.json())
    .then(data => {
        console.log(data);
    });

The response is converted to JSON and can then be used by the application.

24. HTTP POST in React Native

A POST request can send JSON data to the server.


fetch("https://example.com/api/students", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        name: "Amit",
        email: "amit@example.com"
    })
});

25. HTTP Request and Authentication

Authentication information can be included in HTTP headers.

For token-based authentication:


Authorization: Bearer YOUR_TOKEN

The PHP REST API can verify the token before processing a protected request.

26. HTTP and Error Handling

The mobile application should check the response and handle errors.

For example:


if (!response.ok) {
    console.log("API request failed");
}

The application can then display a suitable error message.

27. HTTP Communication Flow


React Native
      ↓
HTTP Request
      ↓
HTTPS / Internet
      ↓
PHP REST API
      ↓
MySQL
      ↓
PHP REST API
      ↓
HTTP Response
      ↓
React Native

This communication pattern is used throughout our REST API and mobile application projects.

28. Important HTTP Concepts

Before continuing with REST API development, remember these HTTP concepts:

  • Request
  • Response
  • HTTP method
  • URL
  • Headers
  • Request body
  • Response body
  • Status code
  • HTTP and HTTPS

29. HTTP in Our REST API Course

HTTP will be used throughout this course.


React Native
      ↓
HTTP / HTTPS
      ↓
PHP REST API
      ↓
MySQL

The upcoming lessons will use HTTP methods, headers, JSON, status codes, and API endpoints to build complete backend services.

30. Final HTTP Communication Model

The most important concept is the HTTP request-response cycle.


                CLIENT
          React Native App
                  ↓
            HTTP Request
                  ↓
        ┌─────────────────┐
        │   REST API      │
        │   PHP Backend   │
        └────────┬────────┘
                 ↓
              MySQL
                 ↓
        ┌─────────────────┐
        │   REST API      │
        │   PHP Backend   │
        └────────┬────────┘
                 ↓
           HTTP Response
                 ↓
          React Native App

Once you understand HTTP, you can understand how REST APIs communicate with React Native applications.

📌 Key Points

  • HTTP stands for HyperText Transfer Protocol.
  • HTTP is commonly used for client-server communication.
  • REST APIs commonly use HTTP.
  • HTTP requests are sent by clients.
  • HTTP responses are sent by servers.
  • GET, POST, PUT, PATCH and DELETE are important HTTP methods.
  • HTTP requests can contain URLs, headers, parameters and bodies.
  • HTTP responses contain status codes, headers and response bodies.
  • JSON is commonly used for REST API data exchange.
  • HTTPS provides encrypted HTTP communication using TLS.
  • HTTP status codes communicate the result of a request.
  • React Native can communicate with APIs using Fetch or Axios.
  • HTTP is the foundation of communication in our REST API course.

🧠 Quick Quiz

Question: What does HTTP stand for?