Lesson 10 of 158 – HTTP Methods
10%

HTTP Methods

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.

Note: The most commonly used HTTP methods in REST API development are GET, POST, PUT, PATCH, and DELETE.

1. What is an HTTP Method?

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.

2. Common HTTP Methods

The main HTTP methods used in our REST API course are:

  • GET – Retrieve data
  • POST – Send or create data
  • PUT – Replace or update a resource
  • PATCH – Partially update a resource
  • DELETE – Delete a resource

3. HTTP Methods and CRUD

HTTP methods are commonly mapped to CRUD operations.

CRUD Operation HTTP Method
Create POST
Read GET
Update PUT / PATCH
Delete DELETE

4. GET Method

The GET method is commonly used to retrieve data from a server.

Example:


GET /api/students

This request can return a list of students.

5. GET a Single Resource

GET can also be used to retrieve a specific resource.


GET /api/students/10

Here, 10 identifies a particular student.

6. POST Method

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.

7. POST 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.

8. PUT Method

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.

9. PUT Request Body

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.

10. PATCH Method

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.

11. PATCH Request Body

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.

12. DELETE Method

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.

13. GET vs POST

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

14. PUT vs PATCH

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.

15. DELETE Example

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.

16. HTTP Methods with Students API


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.

17. HTTP Method and Endpoint

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.

18. HTTP Methods and React Native

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.

19. GET Request in React Native


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.

20. POST Request in React Native


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.

21. PUT Request in React Native


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.

22. PATCH Request in React Native


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.

23. DELETE Request in React Native


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.

24. HTTP Methods and Postman

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.

25. HTTP Methods and Status Codes

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.

26. Choosing the Correct Method

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

27. Complete CRUD Example

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

28. HTTP Methods and API Architecture


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.

29. Important HTTP Method Rules

  • GET is commonly used for retrieving resources.
  • POST is commonly used for creating resources.
  • PUT is commonly used for replacing or updating resources.
  • PATCH is commonly used for partial updates.
  • DELETE is commonly used for removing resources.
  • The endpoint identifies the resource being accessed.
  • The request body can contain data sent to the server.
  • The response contains the result of the operation.

30. Final HTTP Methods Summary

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.

📌 Key Points

  • HTTP methods describe the intended operation of a request.
  • GET is commonly used to retrieve data.
  • POST is commonly used to create or submit data.
  • PUT is commonly used to update or replace a resource.
  • PATCH is commonly used for partial updates.
  • DELETE is commonly used to remove a resource.
  • HTTP methods work together with API endpoints.
  • POST, PUT and PATCH commonly send data in the request body.
  • React Native can send HTTP methods using Fetch or Axios.
  • Postman can be used to test HTTP methods.
  • HTTP status codes communicate the result of an operation.
  • HTTP methods are the foundation of CRUD operations in REST APIs.

🧠 Quick Quiz

Question: Which HTTP method is commonly used to retrieve data from a REST API?