Lesson 16 of 158 – HTTP Status Codes
16%

HTTP Status Codes

HTTP status codes are numbers returned by a web server to describe the result of an HTTP request.

When a React Native application communicates with a REST API, the server sends a status code along with the response. The mobile application can use this code to understand whether the request was successful or whether an error occurred.

Note: HTTP status codes are three-digit numbers. The first digit indicates the general category of the response.

1. What is an HTTP Status Code?

An HTTP status code is a three-digit number returned by a server after processing an HTTP request.

Example:


HTTP/1.1 200 OK

Here, 200 is the status code and OK describes the result.

2. Why Status Codes are Important

Status codes allow the client to understand what happened after sending a request.

For example:

  • 200 means the request was successful.
  • 201 means a resource was created.
  • 400 means the request was invalid.
  • 401 means authentication is required.
  • 404 means the requested resource was not found.
  • 500 means a server-side error occurred.

3. Status Code Categories

HTTP status codes are divided into five main categories.

Range Category
100–199 Informational responses
200–299 Successful responses
300–399 Redirection responses
400–499 Client error responses
500–599 Server error responses

4. 1xx Informational Codes

Status codes from 100 to 199 are informational responses. They indicate that the request has been received or that the communication is continuing.

These responses are less commonly handled directly in basic React Native REST API applications.


1xx
↓
Informational

5. 2xx Success Codes

Status codes from 200 to 299 indicate successful processing of a request.

Common examples include:

  • 200 OK
  • 201 Created
  • 202 Accepted
  • 204 No Content

6. 3xx Redirection Codes

Status codes from 300 to 399 are related to redirection.

They can indicate that the requested resource or response requires another step or location.


3xx
↓
Redirection

Basic REST API development usually focuses more heavily on 2xx, 4xx, and 5xx responses.

7. 4xx Client Error Codes

Status codes from 400 to 499 generally indicate that there is a problem with the request from the client side.

Common examples include:

  • 400 Bad Request
  • 401 Unauthorized
  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 409 Conflict
  • 422 Unprocessable Content

8. 5xx Server Error Codes

Status codes from 500 to 599 generally indicate that the server encountered an error while processing the request.

Common examples include:

  • 500 Internal Server Error
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 504 Gateway Timeout

9. 200 OK

200 OK indicates that the request was successfully processed.

For example, a GET request that successfully retrieves students can return:


HTTP/1.1 200 OK

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

10. 201 Created

201 Created indicates that a new resource was successfully created.

It is commonly used after a successful POST request.


POST /api/students

HTTP/1.1 201 Created

The response can contain information about the newly created resource.

11. 202 Accepted

202 Accepted indicates that the request has been accepted for processing, but processing may not have completed yet.


HTTP/1.1 202 Accepted

This can be useful when an operation is handled asynchronously.

12. 204 No Content

204 No Content indicates that the request was successfully processed but there is no response body to return.

It can be used for operations such as successful updates or deletions.


HTTP/1.1 204 No Content

13. 400 Bad Request

400 Bad Request indicates that the server cannot properly process the request because the request is invalid.

For example:

  • Invalid JSON
  • Missing required data
  • Invalid request format
  • Invalid parameter value

HTTP/1.1 400 Bad Request

14. 401 Unauthorized

401 Unauthorized indicates that the request requires valid authentication credentials.

For example, a protected API may require an access token.


HTTP/1.1 401 Unauthorized

The React Native application can respond by asking the user to log in or refresh authentication.

15. 403 Forbidden

403 Forbidden indicates that the server understood the request but refuses to authorize it.

For example, a normal user may not have permission to delete a student record.


HTTP/1.1 403 Forbidden

This is commonly related to authorization or permissions.

16. 404 Not Found

404 Not Found indicates that the requested resource could not be found.

Example:


GET /api/students/9999

If student 9999 does not exist, the API may return:


HTTP/1.1 404 Not Found

17. 405 Method Not Allowed

405 Method Not Allowed indicates that the HTTP method used is not supported for the requested resource.

For example, an endpoint may support GET but not DELETE.


HTTP/1.1 405 Method Not Allowed

18. 409 Conflict

409 Conflict indicates that the request conflicts with the current state of the resource.

For example, an API might return 409 when a registration attempts to create a record that conflicts with an existing unique value.


HTTP/1.1 409 Conflict

19. 422 Unprocessable Content

422 Unprocessable Content can be used when the server understands the request but cannot process the submitted data because it does not satisfy the required validation rules.

For example:

  • Invalid email format
  • Invalid field value
  • Validation failure

HTTP/1.1 422 Unprocessable Content

20. 500 Internal Server Error

500 Internal Server Error indicates that the server encountered an unexpected condition while processing the request.

For a PHP REST API, this may be caused by an unexpected application error, database problem, or server configuration issue.


HTTP/1.1 500 Internal Server Error

21. 503 Service Unavailable

503 Service Unavailable indicates that the server is currently unable to handle the request.

This can happen when a service is temporarily unavailable or overloaded.


HTTP/1.1 503 Service Unavailable

22. Checking Status Code in Fetch

React Native can check the HTTP response using the Fetch API.


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

console.log(response.status);

if (response.ok) {
    console.log("Request successful");
}

The status property contains the numeric HTTP status code.

23. Handling Different Status Codes

The mobile application can handle different status codes separately.


if (response.status === 200) {

    console.log("Success");

} else if (response.status === 401) {

    console.log("Please login");

} else if (response.status === 404) {

    console.log("Resource not found");

} else if (response.status >= 500) {

    console.log("Server error");

}

24. Status Codes and REST Methods

Operation Common Success Code
GET 200 OK
POST 201 Created
PUT 200 OK or 204 No Content
PATCH 200 OK or 204 No Content
DELETE 200 OK or 204 No Content

The exact status code depends on the API's design and response requirements.

25. Status Codes in PHP

PHP can send an HTTP status code using http_response_code().


http_response_code(201);

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

echo json_encode([
    "success" => true,
    "message" => "Student created"
]);

This allows the REST API to communicate the result clearly to the client.

26. Status Code and JSON Error Response

An API can send both an HTTP status code and a JSON error message.


http_response_code(404);

echo json_encode([
    "success" => false,
    "message" => "Student not found"
]);

React Native can check the status code and then read the JSON message.

27. Status Codes and Postman

Postman displays the HTTP status code after an API request is sent.

For example, after sending a request, Postman may show:


200 OK

or:


404 Not Found

This makes Postman useful for testing REST APIs before connecting them to React Native.

28. Status Codes in a Mobile App


React Native
      ↓
Send API Request
      ↓
PHP REST API
      ↓
Process Request
      ↓
HTTP Status Code
      +
JSON Response
      ↓
React Native
      ↓
Show Success or Error

Status codes help the mobile application decide how to respond to the server's result.

29. Common Status Code Mistakes

  • Returning 200 for every type of response.
  • Ignoring the HTTP status code in React Native.
  • Returning the wrong status for validation errors.
  • Using a success status when the requested resource does not exist.
  • Not returning meaningful JSON error messages.
  • Not handling 401 authentication errors.
  • Not handling 403 authorization errors.
  • Not handling 500 server errors.

30. Complete HTTP Status Code Concept

HTTP status codes provide a standard way for a REST API to communicate the result of an HTTP request.


2xx → Success
3xx → Redirection
4xx → Client Error
5xx → Server Error

A React Native application can use these status codes together with JSON responses to decide whether to display data, show validation messages, request authentication, or report a server problem.

📌 Key Points

  • HTTP status codes are three-digit numbers.
  • 1xx codes are informational responses.
  • 2xx codes indicate successful responses.
  • 3xx codes are related to redirection.
  • 4xx codes generally indicate client-side request problems.
  • 5xx codes generally indicate server-side problems.
  • 200 OK is commonly used for successful requests.
  • 201 Created is commonly used when a resource is created.
  • 204 No Content indicates successful processing without a response body.
  • 400 Bad Request indicates an invalid request.
  • 401 Unauthorized indicates that authentication is required.
  • 403 Forbidden indicates that the request is not authorized.
  • 404 Not Found indicates that a requested resource was not found.
  • 500 Internal Server Error indicates an unexpected server problem.
  • React Native can check the response status using Fetch.
  • PHP can set status codes using http_response_code().
  • Postman can be used to test and inspect HTTP status codes.

🧠 Quick Quiz

Question: Which HTTP status code commonly indicates that a request was successful?