HTTP methods are used to tell a server what type of operation a client wants to perform. REST APIs use HTTP methods to work with resources such as users, students, products, courses, and orders.
When developing a React Native application, HTTP methods allow the mobile application to communicate with the PHP REST API in a standardized way.
An HTTP method indicates the intended action of an HTTP request.
For example:
GET /api/students
Here, GET tells the server that the client wants to retrieve information.
The main HTTP methods used in our REST API course are:
HTTP methods are commonly mapped to CRUD operations.
| CRUD Operation | HTTP Method |
|---|---|
| Create | POST |
| Read | GET |
| Update | PUT / PATCH |
| Delete | DELETE |
The GET method is commonly used to retrieve data from a server.
Example:
GET /api/students
This request can return a list of students.
GET can also be used to retrieve a specific resource.
GET /api/students/10
Here, 10 identifies a particular student.
The POST method is commonly used to send data to a server and create a new resource.
Example:
POST /api/students
The new student information can be included in the request body.
A POST request can contain JSON data in its request body.
{
"name": "Rahul",
"email": "rahul@example.com",
"course": "React Native"
}
The PHP REST API can validate this information before storing it in MySQL.
The PUT method is commonly used to update or replace an existing resource.
Example:
PUT /api/students/10
The server can use the ID to identify the resource that should be updated.
A PUT request can contain the information needed to update a resource.
{
"name": "Rahul Kumar",
"email": "rahulk@example.com",
"course": "React Native"
}
The backend can update the corresponding database record.
The PATCH method is commonly used when only part of an existing resource needs to be updated.
Example:
PATCH /api/students/10
For example, only the email address could be changed.
A PATCH request may contain only the fields that need to be changed.
{
"email": "newemail@example.com"
}
The server can update only the specified field.
The DELETE method is commonly used to remove a resource.
Example:
DELETE /api/students/10
The API can process the request and delete the corresponding record from the database.
| GET | POST |
|---|---|
| Commonly retrieves data | Commonly submits or creates data |
| Data can be represented in the URL | Data is commonly sent in the request body |
| Often used for reading resources | Often used for creating resources |
| PUT | PATCH |
|---|---|
| Commonly represents a complete replacement/update | Commonly represents a partial update |
| May contain the complete resource representation | May contain only changed fields |
| Example: update student | Example: update only email |
The exact behavior depends on how the API is designed.
Suppose the mobile application wants to delete student number 25.
DELETE /api/students/25
The PHP API can identify student 25 and perform the required database operation.
GET /api/students
GET /api/students/10
POST /api/students
PUT /api/students/10
PATCH /api/students/10
DELETE /api/students/10
These endpoints provide a simple example of CRUD operations for a student resource.
The HTTP method and endpoint work together to describe the requested operation.
GET /api/students
The endpoint identifies the resource, while GET indicates that the client wants to retrieve it.
DELETE /api/students/10
Here, DELETE indicates that the client wants to remove the resource identified by 10.
React Native can send different HTTP methods using Fetch or Axios.
fetch(url, {
method: "POST"
});
The method property tells Fetch which HTTP method should be used.
fetch("https://example.com/api/students", {
method: "GET"
})
.then(response => response.json())
.then(data => {
console.log(data);
});
GET is commonly used when the application needs to retrieve data.
fetch("https://example.com/api/students", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "Rahul",
email: "rahul@example.com"
})
});
The request sends JSON data to the API.
fetch("https://example.com/api/students/10", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "Rahul Kumar",
email: "rahulk@example.com"
})
});
This request can be used to update student number 10.
fetch("https://example.com/api/students/10", {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
email: "newemail@example.com"
})
});
Only the required field can be sent for a partial update.
fetch("https://example.com/api/students/10", {
method: "DELETE"
})
.then(response => response.json())
.then(data => {
console.log(data);
});
This can request deletion of student number 10.
Postman can be used to test different HTTP methods before connecting the API to React Native.
For example:
GET /api/students
POST /api/students
PUT /api/students/10
PATCH /api/students/10
DELETE /api/students/10
Postman testing will be covered in detail in the upcoming lessons.
The server can return an appropriate HTTP status code after processing a method.
| Operation | Possible Success Status |
|---|---|
| GET | 200 OK |
| POST | 201 Created |
| PUT | 200 OK |
| PATCH | 200 OK |
| DELETE | 200 OK or 204 No Content |
The exact status code depends on how the API is designed.
Use the HTTP method according to the operation you want to perform.
Need to read data?
↓
GET
Need to create data?
↓
POST
Need to update data?
↓
PUT / PATCH
Need to delete data?
↓
DELETE
Consider a student management application.
CREATE
POST /api/students
READ
GET /api/students
READ ONE
GET /api/students/10
UPDATE
PUT /api/students/10
PARTIAL UPDATE
PATCH /api/students/10
DELETE
DELETE /api/students/10
React Native
↓
HTTP Method
↓
REST API
↓
PHP Backend
↓
MySQL
↓
JSON Response
↓
React Native
The HTTP method is one of the first pieces of information the API uses to understand the client's requested operation.
HTTP methods provide a standard way to describe operations in REST API communication.
| Method | Purpose | Example |
|---|---|---|
| GET | Retrieve | /api/students |
| POST | Create | /api/students |
| PUT | Update/replace | /api/students/10 |
| PATCH | Partial update | /api/students/10 |
| DELETE | Delete | /api/students/10 |
These methods will be used throughout our PHP REST API and React Native projects.
Question: Which HTTP method is commonly used to retrieve data from a REST API?