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.
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
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.
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.
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.
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"
}
An HTTP request can contain several important components.
The exact parts used depend on the request.
An HTTP response can contain:
Status Code
↓
Headers
↓
Response Body
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 |
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.
POST is commonly used to send data to the server.
POST /api/students
The request can contain student information in the request body.
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.
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.
DELETE is commonly used to remove a resource.
DELETE /api/students/10
The server can process the request and remove the corresponding record.
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.
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.
HTTP headers provide additional information about a request or response.
Example request header:
Content-Type: application/json
Headers can also carry authentication information.
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.
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.
HTTP status codes tell the client what happened while processing the request.
| 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 |
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.
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"
}
}
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.
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"
})
});
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.
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.
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.
Before continuing with REST API development, remember these HTTP concepts:
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.
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.
Question: What does HTTP stand for?