Lesson 29 of 158 – API Response Structure
29%

API Response Structure

An API response is the data that a server sends back to the client after processing an API request. A well-designed REST API usually follows a consistent response structure so that mobile applications can easily understand the result.

When a React Native application communicates with a PHP REST API, the server commonly returns JSON containing information such as success status, message, data, and error details.

Note: A consistent API response structure makes it easier to develop, debug, and maintain React Native applications.

1. What is an API Response?

An API response is the information returned by a server after receiving and processing an API request.


Request
    ↓
REST API
    ↓
Response

The response may contain JSON data, an HTTP status code, and response headers.

2. Basic API Response

A simple JSON API response can look like this:


{
    "message": "Student found"
}

The client receives the JSON object and can use the message in the application.

3. API Response with Success

An API can include a success property to indicate whether an operation was successful.


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

The mobile application can check the success property before processing the returned data.

4. API Response with Data

A response can contain a data property containing the actual resource.


{
    "success": true,
    "data": {
        "id": 1,
        "name": "Rahul",
        "course": "React Native"
    }
}

The data object contains the student information.

5. API Response with Message

A message can provide a human-readable description of the result.


{
    "success": true,
    "message": "Student added successfully"
}

Messages are useful for displaying information to users and for debugging API operations.

6. Response with Student Data


{
    "success": true,
    "message": "Student found",
    "data": {
        "id": 10,
        "name": "Amit",
        "email": "amit@example.com",
        "course": "React Native"
    }
}

This structure is useful when returning one student from an API.

7. Response with Multiple Records

When an API returns multiple records, the data property can contain an array.


{
    "success": true,
    "message": "Students found",
    "data": [
        {
            "id": 1,
            "name": "Rahul"
        },
        {
            "id": 2,
            "name": "Amit"
        },
        {
            "id": 3,
            "name": "Priya"
        }
    ]
}

8. API Response and HTTP Status Code

An API response contains both an HTTP status and a response body.


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

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

The status code describes the HTTP result, while the JSON body contains application-level information.

9. Successful GET Response

A successful GET request can return a list of students.


GET /api/students

200 OK

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

10. Successful POST Response

After creating a student, the API can return the created record.


POST /api/students

201 Created

{
    "success": true,
    "message": "Student created successfully",
    "data": {
        "id": 15,
        "name": "Rahul",
        "course": "React Native"
    }
}

11. Successful PUT Response

A PUT request can return the updated resource.


PUT /api/students/15

200 OK

{
    "success": true,
    "message": "Student updated successfully",
    "data": {
        "id": 15,
        "name": "Rahul Kumar",
        "course": "React Native"
    }
}

12. Successful DELETE Response

A DELETE request can return a message indicating that the record was removed.


DELETE /api/students/15

200 OK

{
    "success": true,
    "message": "Student deleted successfully"
}

13. Error Response

When an API request fails, the response can contain success set to false and a useful error message.


{
    "success": false,
    "message": "Student not found"
}

The HTTP status code can provide additional information about the error.

14. Error Response with Status Code


HTTP/1.1 404 Not Found
Content-Type: application/json

{
    "success": false,
    "message": "Student not found"
}

The HTTP status and JSON body together provide useful information to the client.

15. Validation Error Response

An API can return validation errors when required information is missing.


{
    "success": false,
    "message": "Validation failed",
    "errors": {
        "name": "Name is required",
        "email": "Email is required"
    }
}

This structure allows the mobile application to identify individual field errors.

16. API Response with Errors Array

Some APIs may return multiple errors inside an array.


{
    "success": false,
    "message": "Validation failed",
    "errors": [
        "Name is required",
        "Email is invalid"
    ]
}

The exact error structure should remain consistent throughout an API.

17. API Response Structure with Pagination

When an API returns a large list, pagination information can be included in the response.


{
    "success": true,
    "data": [
        {
            "id": 1,
            "name": "Rahul"
        },
        {
            "id": 2,
            "name": "Amit"
        }
    ],
    "pagination": {
        "page": 1,
        "limit": 10,
        "total": 50
    }
}

18. API Response Metadata

An API can include additional information called metadata.


{
    "success": true,
    "data": [],
    "meta": {
        "page": 1,
        "total": 100
    }
}

Metadata can provide information that helps the client understand the returned data.

19. Consistent Response Structure

A REST API should use a consistent response structure across its endpoints.

For example, successful responses can consistently use:


{
    "success": true,
    "message": "...",
    "data": {}
}

Error responses can consistently use:


{
    "success": false,
    "message": "..."
}

20. API Response in PHP

PHP can create a JSON response using an array and json_encode().


header(
    "Content-Type: application/json"
);

$response = [
    "success" => true,
    "message" => "Student found",
    "data" => [
        "id" => 1,
        "name" => "Rahul"
    ]
];

echo json_encode($response);

21. API Response in React Native

React Native can read the JSON response using the Fetch API.


const response = await fetch(
    "https://example.com/api/students"
);

const result = await response.json();

console.log(result);

The returned object can then be used to update the mobile interface.

22. Checking Success in React Native

If the API uses a success property, the mobile application can check it before using the data.


if (result.success) {

    console.log(result.data);

} else {

    console.log(result.message);

}

23. API Response with TypeScript Interface

TypeScript can describe the structure of an API response with an interface.


interface Student {
    id: number;
    name: string;
    course: string;
}

interface StudentResponse {
    success: boolean;
    message: string;
    data: Student;
}

This helps TypeScript understand the expected response structure.

24. API List Response with TypeScript

For multiple students, the data property can be an array.


interface StudentResponse {
    success: boolean;
    message: string;
    data: Student[];
}

This is useful when displaying API data in a React Native FlatList.

25. Common API Response Mistakes

  • Returning different structures from similar endpoints.
  • Not setting the correct response Content-Type.
  • Returning unclear error messages.
  • Mixing arrays and objects without a clear structure.
  • Not using appropriate HTTP status codes.
  • Returning HTML errors from a JSON API.
  • Not validating data before returning it.
  • Changing response property names unexpectedly.

26. Good API Response Structure

A practical JSON response can contain the following properties:

Property Purpose
success Indicates whether the operation succeeded.
message Provides information about the result.
data Contains the requested or created resource.
errors Contains validation or processing errors.
pagination Contains information about paginated data.

27. Testing API Response in Postman

Postman can be used to inspect the complete response from an API.

  1. Open Postman.
  2. Select the HTTP method.
  3. Enter the API URL.
  4. Send the request.
  5. Check the HTTP status code.
  6. Check the response headers.
  7. Check the JSON response body.

This is useful for testing a PHP REST API before connecting it to React Native.

28. Complete API Response Flow


React Native
      ↓
HTTP Request
      ↓
PHP REST API
      ↓
Validate Request
      ↓
Database Operation
      ↓
Create Response
      ↓
HTTP Status Code
      ↓
JSON Response
      ↓
React Native

The mobile application receives the response and uses its structure to update the user interface.

29. Complete Student API Response

Example successful response:


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

{
    "success": true,
    "message": "Student found",
    "data": {
        "id": 10,
        "name": "Rahul",
        "email": "rahul@example.com",
        "course": "React Native"
    }
}

Example error response:


HTTP/1.1 404 Not Found
Content-Type: application/json

{
    "success": false,
    "message": "Student not found"
}

30. Complete API Response Structure

A REST API response should provide enough information for the client to understand what happened. A common structure includes a success indicator, message, data, and optional error or pagination information.


{
    "success": true,
    "message": "Students found",
    "data": [
        {
            "id": 1,
            "name": "Rahul",
            "course": "React Native"
        },
        {
            "id": 2,
            "name": "Amit",
            "course": "React Native"
        }
    ],
    "pagination": {
        "page": 1,
        "limit": 10,
        "total": 2
    }
}

A consistent structure makes REST API integration easier for React Native applications and makes the API easier to maintain.

📌 Key Points

  • An API response is the data returned by the server.
  • API responses commonly use JSON.
  • The HTTP status code describes the result of the HTTP request.
  • The response body contains application data.
  • A success property can indicate whether an operation succeeded.
  • A message property can describe the result.
  • A data property can contain a resource or array of resources.
  • Error responses can contain an errors property.
  • Pagination information can be included in the response.
  • PHP can create JSON responses using json_encode().
  • React Native can read JSON using response.json().
  • TypeScript interfaces can describe API response structures.
  • Postman is useful for testing API responses.
  • Consistent response structures make API integration easier.
  • The response Content-Type should correctly describe the response body.

🧠 Quick Quiz

Question: Which property is commonly used to contain the actual data returned by a REST API?