HTTP stands for HyperText Transfer Protocol.
HTTP is a protocol used for communication between a client and a server.
In IoT, HTTP is especially useful because an ESP32 can communicate with a web server or API using HTTP.
For example, our soil moisture project can work like this:
Soil Moisture Sensor → ESP32 → Wi-Fi → HTTP → Laravel API → Database
The ESP32 reads the soil moisture and sends the reading to our Laravel application.
Client and Server
Before understanding HTTP methods, we need to understand two basic terms.
Client
A client is a device or application that sends a request to a server.
Examples:
- Web browser
- Mobile application
- Arduino
- ESP32
Server
A server receives requests and sends responses.
Examples:
- Laravel application
- Web server
- API server
Practical Example
Suppose our Laravel application has this API:
https://example.com/api/soil
Our ESP32 wants to get the latest soil moisture value.
The ESP32 is the client.
The Laravel application is the server.
The communication is:
ESP32 → Request → Laravel
ESP32 ← Response ← Laravel
HTTP Request
An HTTP request is a message sent by a client to a server.
An HTTP request can contain:
- HTTP method
- URL
- Headers
- Data
For example:
GET /api/soil
This means:
"Give me the soil data."
HTTP Response
After receiving the request, the server sends an HTTP response.
A response contains a status code and may contain data.
For example:
200 OK
and:
This means the request was successful and the server returned the moisture value.
HTTP Methods
The four important HTTP methods we need to learn are:
- GET
- POST
- PUT
- DELETE
A simple way to remember them is:
GET → Read
POST → Create / Send
PUT → Update
DELETE → Remove
1. GET
GET is used to retrieve or read data from a server.
Real-Life Example
Imagine you open a website and want to see your profile.
Your browser sends a GET request:
GET /api/user/10
The server returns the user's information.
IoT Example
Suppose our ESP32 wants to know the current pump status.
It sends:
GET /api/pump/status
The Laravel server might respond:
The ESP32 can then decide what to do.
Arduino/ESP32 Concept
The flow is:
ESP32 → GET Request → Laravel
Laravel → Response → ESP32
Another Example
Suppose our website wants the latest soil moisture reading.
The website sends:
GET /api/soil/latest
Laravel returns:
The website can display:
Soil Moisture: 320
Important Points
GET is mainly used for reading data.
It should not normally be used to create or modify resources.
2. POST
POST is commonly used to send data to a server and create a new resource.
This is one of the most important HTTP methods for IoT.
Real-Life Example
Suppose a registration form asks for:
Name: Hemant
Email: example@email.com
When you submit the form, the browser can send this information to the server using POST.
IoT Example
Suppose the ESP32 reads:
Moisture = 320
Temperature = 27.5°C
Humidity = 62%
The ESP32 can send this data to Laravel using:
POST /api/sensors
The request body might contain:
Laravel receives the data and can store it in the database.
Complete Flow
Soil Sensor
↓
ESP32
↓
POST Request
↓
Laravel API
↓
MySQL Database
Practical Example
Suppose the database contains:
Device: ESP32-01
Moisture: 320
Temperature: 27.5
Humidity: 62
The ESP32 can send this information every 10 seconds.
Important Points
POST is commonly used to send data to the server.
The data is normally placed in the request body.
3. PUT
PUT is commonly used to update an existing resource.
Real-Life Example
Suppose your application already has a user:
Name: Hemant
Later, the user changes the name to:
Name: Hemant Singh
The application can send a PUT request to update the existing user.
For example:
PUT /api/users/10
with:
IoT Example
Suppose our IoT application has a moisture threshold.
Current threshold:
500
This means:
Moisture > 500 → Soil considered dry
Now the user changes the threshold to:
450
The application can send:
PUT /api/settings/1
with:
Laravel updates the existing setting in the database.
Complete Flow
Web Dashboard
↓
PUT Request
↓
Laravel API
↓
Update Database
Important Points
PUT is commonly used to update an existing resource.
The resource is normally identified using the URL.
For example:
PUT /api/sensors/10
means:
"Update sensor number 10."
4. DELETE
DELETE is used to remove an existing resource.
Real-Life Example
Suppose your application contains:
User ID: 10
If you click Delete, the application can send:
DELETE /api/users/10
The server can then remove that user.
IoT Example
Suppose your IoT system contains three sensors:
Sensor 1 → Garden
Sensor 2 → Greenhouse
Sensor 3 → Balcony
Suppose Sensor 2 is no longer being used.
The application can send:
DELETE /api/sensors/2
Laravel can then remove that sensor from the system.
Complete Flow
Web Dashboard
↓
DELETE Request
↓
Laravel API
↓
Remove Sensor
Important Points
DELETE is used when a resource needs to be removed.
GET vs POST vs PUT vs DELETE
Suppose our application manages soil moisture sensors.
GET
GET /api/sensors
Meaning:
"Give me the sensors."
POST
POST /api/sensors
Meaning:
"Create a new sensor."
Example:
PUT
PUT /api/sensors/10
Meaning:
"Update sensor 10."
Example:
DELETE
DELETE /api/sensors/10
Meaning:
"Delete sensor 10."
Practical IoT Example
Let's imagine that we have built a complete soil monitoring system.
Our components are:
Soil Moisture Sensor
ESP32
Wi-Fi
Laravel
MySQL
The ESP32 reads:
Moisture = 350
It sends:
POST /api/soil-data
with:
Laravel receives the data and stores it in MySQL.
Later, the dashboard requests the latest value:
GET /api/soil-data/latest
Laravel responds:
The dashboard displays:
Current Moisture: 350
Now suppose the user changes the sensor name.
The dashboard sends:
PUT /api/devices/ESP32-01
with:
Later, if the device is removed:
DELETE /api/devices/ESP32-01
This is how the four HTTP methods can work together in a real IoT application.
HTTP Status Codes
Whenever the server responds, it usually sends an HTTP status code.
These codes tell us what happened.
200 OK
The request was successful.
Example:
ESP32 requests sensor data.
Server responds:
200 OK
201 Created
A new resource was successfully created.
Example:
ESP32 sends a new sensor record.
Server responds:
201 Created
400 Bad Request
The request contains invalid or incorrect data.
Example:
The ESP32 sends:
when the server expects a number.
The server may respond:
400 Bad Request
401 Unauthorized
Authentication is required or the authentication information is invalid.
403 Forbidden
The server understood the request but does not allow the operation.
404 Not Found
The requested resource does not exist.
Example:
GET /api/sensors/9999
If sensor 9999 does not exist:
404 Not Found
500 Internal Server Error
Something went wrong on the server.
JSON
IoT applications commonly use JSON to exchange data.
JSON is a simple text format for representing structured data.
Example:
The ESP32 can send this JSON to Laravel using POST.
Laravel can return JSON to the ESP32 or web dashboard.
HTTP Headers
HTTP requests can contain headers.
Headers provide additional information about the request.
One very common header is:
Content-Type
For JSON data:
Content-Type: application/json
This tells the server that the request body contains JSON.
Another important header is:
Authorization
It can be used when an API requires authentication.
REST API
HTTP methods are commonly used with REST APIs.
A REST API provides endpoints that allow clients to work with resources.
For our IoT application, we might have:
GET /api/sensors
Get all sensors.
GET /api/sensors/10
Get sensor 10.
POST /api/sensors
Create a new sensor.
PUT /api/sensors/10
Update sensor 10.
DELETE /api/sensors/10
Delete sensor 10.