Lesson 8 of 158 – Request and Response
8%

Request and Response

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.

Note: In our mobile application projects, React Native will send requests to the PHP REST API, and the PHP API will return responses, usually using JSON data.

1. What is a Request?

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

2. What is a Response?

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"
}

3. Request-Response Cycle

The basic communication process is called the request-response cycle.


Client
  ↓
Request
  ↓
Server
  ↓
Processing
  ↓
Response
  ↓
Client

4. React Native as the 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

5. PHP REST API as the Server

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

6. Basic Request Example

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.

7. Basic Response Example

After processing the request, the API can return a JSON response.


[
    {
        "id": 1,
        "name": "Rahul"
    },
    {
        "id": 2,
        "name": "Amit"
    }
]

8. Parts of an HTTP Request

An HTTP request can contain several important parts.

  • HTTP method
  • URL
  • Headers
  • Query parameters
  • Request body

Not every request needs every possible part.

9. HTTP Method

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

10. URL in a Request

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.

11. Request Headers

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.

12. Request Body

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"
}

13. Query Parameters

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.

14. Path Parameters

A path parameter can be used to identify a specific resource.

Example:


GET /api/students/10

Here, 10 can represent the student ID.

15. Request Example with JSON

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.

16. Parts of an HTTP Response

An HTTP response can contain:

  • Status code
  • Response headers
  • Response body

The response tells the client what happened after processing the request.

17. Response Status Code

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

18. Response Headers

Response headers provide additional information about the response.

For example:


Content-Type: application/json

This indicates that the response body contains JSON data.

19. Response Body

The response body contains the actual data returned by the server.

Example:


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

20. Successful Response

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"
    }
}

21. Error Response

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.

22. Login Request and Response

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"
}

23. Authentication Request

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.

24. React Native 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.

25. React Native POST Request

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"
    })
});

26. Loading State

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

27. Error Handling

The client should handle errors that occur during API communication.

Possible errors include:

  • No internet connection
  • Invalid request
  • Unauthorized request
  • Resource not found
  • Server error
  • Invalid JSON response

28. Complete Request-Response Flow


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

29. Important Difference Between Request and Response

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

30. Final Request-Response Model

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.

📌 Key Points

  • A request is sent from the client to the server.
  • A response is sent from the server to the client.
  • React Native acts as the client in our mobile applications.
  • PHP REST API acts as the backend server.
  • MySQL stores application data.
  • A request can contain a method, URL, headers, parameters, and body.
  • A response contains a status code, headers, and response body.
  • JSON is commonly used in REST API responses.
  • GET is commonly used to retrieve data.
  • POST is commonly used to send or create data.
  • PUT and PATCH are commonly used for updates.
  • DELETE is commonly used to remove data.
  • Loading and error states should be handled in React Native.
  • The request-response cycle is the foundation of API communication.

🧠 Quick Quiz

Question: Who normally sends an HTTP response in a REST API communication?