Request and response are the two basic parts of communication between a client and a server. When a React Native application needs data or wants to perform an operation, it sends a request to the REST API. The server processes that request and sends a response back.
Understanding requests and responses is essential before learning HTTP methods, JSON, authentication, and React Native API integration.
A request is a message sent by a client to a server asking the server to perform an operation or provide information.
For example, a mobile application can request a list of students.
GET /api/students
A response is the message returned by the server after processing a request.
For example, the server can return student information in JSON format.
{
"id": 1,
"name": "Rahul",
"course": "React Native"
}
The basic communication process is called the request-response cycle.
Client
↓
Request
↓
Server
↓
Processing
↓
Response
↓
Client
In our projects, the React Native mobile application acts as the client.
When the user performs an action, React Native can send a request to the REST API.
User
↓
React Native
↓
API Request
Our PHP REST API will receive requests from the React Native application.
The PHP backend can validate the request, perform business logic, communicate with MySQL, and create a response.
React Native
↓
PHP REST API
↓
MySQL
Suppose the mobile application wants to retrieve all students.
GET /api/students
The API receives the request and can query the database for student records.
After processing the request, the API can return a JSON response.
[
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Amit"
}
]
An HTTP request can contain several important parts.
Not every request needs every possible part.
The HTTP method tells the server what type of operation the client is requesting.
| Method | Common Purpose |
|---|---|
| GET | Retrieve data |
| POST | Send or create data |
| PUT | Update data |
| PATCH | Partially update data |
| DELETE | Delete data |
The URL identifies the server and endpoint to which the request is sent.
Example:
https://example.com/api/students
Here, /api/students represents the students endpoint.
Headers provide additional information about an HTTP request.
For example, a request may specify that it is sending JSON data.
Content-Type: application/json
Authentication information can also be sent using headers.
The request body contains data sent from the client to the server. It is commonly used with POST, PUT, and PATCH requests.
Example:
{
"name": "Amit",
"email": "amit@example.com"
}
Query parameters are values included in the URL after a question mark.
Example:
GET /api/students?course=ReactNative
The server can use the parameter to filter the results.
A path parameter can be used to identify a specific resource.
Example:
GET /api/students/10
Here, 10 can represent the student ID.
A mobile application can send JSON data to the API.
POST /api/students
Content-Type: application/json
{
"name": "Rahul",
"email": "rahul@example.com",
"course": "React Native"
}
The PHP API can read and validate this data.
An HTTP response can contain:
The response tells the client what happened after processing the request.
The HTTP status code indicates the result of the request.
| Status | Meaning |
|---|---|
| 200 | Successful request |
| 201 | Resource created |
| 400 | Bad request |
| 401 | Unauthorized |
| 404 | Resource not found |
| 500 | Server error |
Response headers provide additional information about the response.
For example:
Content-Type: application/json
This indicates that the response body contains JSON data.
The response body contains the actual data returned by the server.
Example:
{
"success": true,
"message": "Student found",
"data": {
"id": 10,
"name": "Rahul"
}
}
Suppose the client requests a student successfully.
GET /api/students/10
The server may return:
HTTP/1.1 200 OK
{
"success": true,
"data": {
"id": 10,
"name": "Rahul"
}
}
If the requested student does not exist, the API can return an error response.
HTTP/1.1 404 Not Found
{
"success": false,
"message": "Student not found"
}
The React Native application can use this response to show an appropriate message.
A login operation is another common example of request and response communication.
POST /api/login
{
"email": "rahul@example.com",
"password": "123456"
}
The server can return:
{
"success": true,
"message": "Login successful",
"token": "YOUR_TOKEN"
}
After login, a client may send an authentication token in a request header when accessing a protected endpoint.
GET /api/profile
Authorization: Bearer YOUR_TOKEN
The server can verify the token before processing the request.
React Native can use the Fetch API to send a request.
fetch("https://example.com/api/students")
.then(response => response.json())
.then(data => {
console.log(data);
});
The returned response can then be used by the mobile application.
A React Native application can send JSON data using a POST request.
fetch("https://example.com/api/students", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "Amit",
email: "amit@example.com"
})
});
The mobile application may need some time to receive the server response.
During this period, React Native can display a loading indicator.
Loading...
↓
API Request
↓
Response
↓
Display Data
The client should handle errors that occur during API communication.
Possible errors include:
User
↓
React Native Screen
↓
Create HTTP Request
↓
REST API
↓
PHP Backend
↓
Validate Request
↓
MySQL Query
↓
Database Result
↓
PHP Creates Response
↓
HTTP Response
↓
React Native
↓
Update Screen
| Request | Response |
|---|---|
| Sent by client | Sent by server |
| Asks server to perform an operation | Reports the result of the operation |
| Can contain request body | Can contain response body |
| Uses request headers | Uses response headers |
| Contains HTTP method | Contains HTTP status code |
The most important concept in REST API development is the request-response cycle.
CLIENT
React Native App
|
| Request
↓
┌──────────────┐
│ REST API │
│ PHP Backend │
└──────┬───────┘
|
| Database Query
↓
┌──────────────┐
│ MySQL │
└──────┬───────┘
|
| Result
↓
┌──────────────┐
│ REST API │
└──────┬───────┘
|
| JSON Response
↓
CLIENT
React Native App
Once you understand this cycle, concepts such as GET, POST, JSON, headers, authentication, and API errors become much easier to understand.
Question: Who normally sends an HTTP response in a REST API communication?