0% found this document useful (0 votes)
20 views6 pages

HTTP Basics Complete Notes

HTTP (Hypertext Transfer Protocol) is a request-response protocol used for communication between clients and servers, characterized by its stateless nature. The core HTTP methods include GET, POST, PUT, PATCH, and DELETE, each serving specific purposes such as retrieving or modifying data. Understanding HTTP is essential for web development, particularly when working with frameworks like Laravel, as it directly influences routing, controllers, and API interactions.

Uploaded by

wisdom Ng'oma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

HTTP Basics Complete Notes

HTTP (Hypertext Transfer Protocol) is a request-response protocol used for communication between clients and servers, characterized by its stateless nature. The core HTTP methods include GET, POST, PUT, PATCH, and DELETE, each serving specific purposes such as retrieving or modifying data. Understanding HTTP is essential for web development, particularly when working with frameworks like Laravel, as it directly influences routing, controllers, and API interactions.

Uploaded by

wisdom Ng'oma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HTTP BASICS COMPLETE NOTES - (GET, POST, PUT, DELETE, PATCH)

1. What Exactly Is HTTP?

HTTP stands for Hypertext Transfer Protocol. It is the language that browsers, mobile
apps, and servers use to communicate with each other.

To understand HTTP well, imagine human communication:

• You ask someone for information.


• They respond with an answer.

HTTP works exactly the same way.

Key properties of HTTP

1. It is a Request–Response protocol.
A client (browser/app) sends a request → server sends back a response.

2. It is stateless.
Every request is treated as new, with no memory of previous requests.
If you refresh a page, the server sees you like a brand-new visitor unless extra tools (cookies,
sessions, tokens) are used.

3. It is the foundation for how Laravel routes and APIs work.

Understanding HTTP is not optional—it is the foundation of modern web development.

2. HTTP Requests and HTTP Responses

HTTP Request → what the client sends

A request contains:

• Method (GET, POST, PUT…)


• URL
• Headers (metadata)
• Optional body (for POST/PUT/PATCH)

Example request:

POST /login
Content-Type: application/json

{
"email": "user@[Link]",
"password": "secret"
}

HTTP Response → what the server returns


A response contains:

• Status code (200, 404, 500…)


• Headers
• Body (HTML, JSON, images, etc.)

Example response:

HTTP/1.1 200 OK
Content-Type: application/json

{
"message": "Login successful"
}

This constant exchange is the heartbeat of the web.

3. The Core HTTP Methods

Although HTTP has many verbs, five form the backbone of real web apps:

Method Meaning Typical Use


GET Retrieve data Fetching lists, viewing pages
POST Create new data Submit forms, register users
PUT Replace an existing resource Update entire records
PATCH Modify part of a resource Update only selected fields
DELETE Remove a resource Delete users, posts, products

APIs and Laravel routing depend completely on these actions.

4. Deep Explanation of Each HTTP Method

4.1 GET — “Give me data”

GET requests are used purely to retrieve information.

Characteristics:

• Safe (does not change data)


• Cacheable
• Appears in browser history
• No request body (usually)

Examples:

GET /products
GET /users?page=3
GET /search?q=maize

Laravel route:

Route::get('/users', [UserController::class, 'index']);

Typing a URL in your browser automatically sends a GET request.

4.2 POST — “Create something new”

POST is used when sending information to create a new entity.

Used for:

• Registration
• Login
• Uploading files
• Submitting forms

Example:

POST /register
{
"name": "John",
"email": "john@[Link]"
}

Laravel:

Route::post('/register', [AuthController::class, 'store']);

Why POST?
Because you’re adding a new user.

4.3 PUT — “Replace the entire resource”

PUT updates an existing record by replacing it entirely.

Example:

PUT /profile/1
{
"name": "John Doe",
"email": "john@[Link]",
"age": 25
}

Laravel:

Route::put('/profile/{id}', [ProfileController::class, 'update']);


If the record had 5 fields, PUT expects all 5—even if you only want to change one.

4.4 PATCH — “Update only what changed”

PATCH is designed for partial updates.

Example:

PATCH /profile/1
{
"email": "new@[Link]"
}

Laravel:

Route::patch('/profile/{id}', [ProfileController::class, 'updatePartial']);

Difference:

• PUT = replace everything


• PATCH = update only what you send

4.5 DELETE — “Remove something”

Used to delete a resource.

Example:

DELETE /post/10

Laravel:

Route::delete('/post/{id}', [PostController::class, 'destroy']);

5. Why HTTP Is “Stateless”

HTTP does not remember anything.

Example problem:

• You log in
• You visit another page
• Server does not remember you

How do we fix this?

Through:

• Sessions
• Cookies
• Tokens (Laravel Sanctum/Passport)

Laravel uses these to simulate “memory.”

6. The Structure of an HTTP Request

An HTTP request has three main parts:

1. Request Line
GET /products HTTP/1.1

2. Headers

Examples:

User-Agent: Chrome
Accept: application/json
Authorization: Bearer token
Content-Type: application/json

3. Request Body (optional)

Only for POST, PUT, PATCH:

{
"price": 500
}

Laravel reads these using:

$request->input()
$request->header()
$request->method()

7. The Structure of an HTTP Response

A response contains:

1. Status Code

Examples:

• 200 OK
• 201 Created
• 404 Not Found
• 500 Server Error
• 422 Validation Failed

2. Headers
Content-Type: application/json
Cache-Control: no-cache

3. Body
{
"message": "Product updated"
}

Laravel:

return response()->json(['success' => true], 200);

8. Why Understanding HTTP Is Essential for Laravel

Without understanding HTTP:

Laravel routes make no sense.

Examples:
Route::post('/login', ...)
Route::put('/product/{id}', ...)
Route::delete('/post/{id}', ...)

These verbs map directly to HTTP.

Laravel also uses them in:

✔ Controllers

index(), store(), update(), destroy() are tied to HTTP verbs.

✔ Forms

Laravel requires:

• CSRF token for POST/PUT/DELETE


• Spoofing of methods (because HTML only supports GET + POST)

Example:

@method('PUT')

✔ APIs

Route::apiResource() generates routes entirely based on HTTP verbs.

You might also like