REST stands for Representational State Transfer. REST is an architectural style used for designing web services and APIs.
REST APIs allow applications such as React Native mobile apps to communicate with servers using standard HTTP requests.
REST stands for:
Representational State Transfer
It was introduced as an architectural style for designing networked applications.
REST is an architectural style, not a programming language.
It provides principles and guidelines for designing APIs that communicate over a network.
For example, PHP can be used to implement a REST API.
REST APIs commonly use the HTTP protocol for communication.
HTTP provides methods such as:
A REST API is an API designed using REST principles.
It exposes resources through URLs and uses HTTP methods to perform operations on those resources.
GET /api/students
POST /api/students
PUT /api/students/10
DELETE /api/students/10
REST focuses on resources.
A resource can represent an object or collection of data.
Examples:
A REST resource is commonly represented using a URL.
For example:
/api/students
This endpoint can represent a collection of students.
A specific resource can be identified using an ID.
/api/students/10
Here, 10 can represent the ID of a particular student.
GET is commonly used to retrieve resources.
Example:
GET /api/students
This can return a list of students.
POST is commonly used to submit data and create a new resource.
Example:
POST /api/students
The request body can contain the information of a new student.
PUT is commonly used to update an existing resource.
Example:
PUT /api/students/10
This request can update the student with ID 10.
PATCH is commonly used to partially update an existing resource.
Example:
PATCH /api/students/10
It can be used when only selected fields need to be changed.
DELETE is commonly used to remove a resource.
Example:
DELETE /api/students/10
This can remove the student whose ID is 10.
One important REST principle is statelessness.
Each request should contain the information necessary for the server to process that request.
The server does not rely on previous requests to understand the current request.
Suppose a mobile application requests a protected student profile. The request can include an authentication token.
GET /api/profile
Authorization: Bearer TOKEN
The server can use the information in the current request to authenticate and process it.
REST separates the client from the server.
Client
|
| HTTP Request
↓
Server
|
| HTTP Response
↓
Client
In our course, React Native will act as the client and PHP will act as the server-side API.
The client and server can be developed separately.
The React Native application can focus on the user interface while the PHP API handles server-side processing and database operations.
React Native
↓
REST API
↓
PHP
↓
MySQL
REST promotes a consistent way of interacting with resources.
For example, a student resource can be accessed through predictable endpoints.
GET /api/students
GET /api/students/5
POST /api/students
PUT /api/students/5
DELETE /api/students/5
A REST API can return a representation of a resource.
JSON is commonly used for this representation.
{
"id": 5,
"name": "Rahul",
"course": "React Native"
}
The actual database record is not directly exposed to the client.
A REST API can return a JSON response containing a collection of resources.
[
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Amit"
}
]
React Native can convert this response into JavaScript/TypeScript data and display it.
REST APIs commonly map HTTP methods to CRUD operations.
| Operation | HTTP Method |
|---|---|
| Create | POST |
| Read | GET |
| Update | PUT / PATCH |
| Delete | DELETE |
Suppose our application manages student records.
GET /api/students
GET /api/students/1
POST /api/students
PUT /api/students/1
PATCH /api/students/1
DELETE /api/students/1
These endpoints provide a consistent way to work with student resources.
React Native applications can consume REST APIs using Fetch or Axios.
For example:
React Native
↓
GET /api/students
↓
PHP REST API
↓
JSON Response
↓
React Native
PHP can process HTTP requests and create REST API endpoints.
For example, a PHP file can receive a request for students, query MySQL and return JSON.
React Native
↓
PHP API
↓
MySQL
↓
JSON
REST itself does not require MySQL.
However, our projects will use MySQL as the database behind the PHP REST API.
React Native
↓
REST API
↓
PHP
↓
MySQL
REST APIs generally use resource-oriented URLs.
For example:
/api/students
/api/users
/api/products
/api/orders
A specific resource can be identified using an ID.
/api/students/10
/api/users/5
/api/products/20
REST APIs use HTTP status codes to communicate the result of a request.
We will study HTTP status codes in detail in a later lesson.
REST APIs may contain sensitive application data, so security is important.
Our course will later cover topics such as:
Consider a React Native student application.
User taps "View Students"
↓
React Native sends GET request
↓
GET /api/students
↓
PHP processes request
↓
PHP queries MySQL
↓
MySQL returns records
↓
PHP creates JSON response
↓
React Native displays students
Important REST concepts include:
These concepts help us design organized and predictable APIs.
REST will be the foundation for connecting our React Native mobile applications with the backend.
React Native + TypeScript
↓
Fetch / Axios
↓
REST API
↓
PHP Backend
↓
MySQL
In the upcoming lessons, we will learn HTTP methods, API endpoints, JSON, Postman, PHP REST API development and eventually connect the API to React Native.
Question: What does REST stand for?