HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. (HyperText Transfer ProtocolWhat is protocol?An agreed-upon set of rules for how two systems communicate, defining the format of messages and the expected sequence of exchanges.) is how browsers and servers talk to each other. Think of it as the grammar and vocabulary of web communication, it defines exactly how messages should be formatted and what they mean. Every webpage you visit, every APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. call your app makes, every image that loads, it all happens through HTTP.
HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. methods: what do you want to do?
Methods describe what action you want to perform. They're like verbs in a sentence.
GET: give me data
GET /users/123- Reads data without modifying anything
- No request body (nothing to send)
- Can be bookmarked and cached by browsers
- Used for: loading pages, fetching profiles, searching
GET requests are safe and idempotentWhat is idempotent?An operation that produces the same result whether you perform it once or multiple times, making retries safe., calling them multiple times has the same effect as calling once. That's why browsers can retry failed GET requests without worry.
POST: create something new
POST /users
Content-Type: application/json
{ "name": "Alice", "email": "alice@example.com" }- Creates a new resource
- Has a request body with the data to create
- Not idempotent, running twice creates two things
- Used for: signup forms, creating posts, submitting data
PUT: update this completely
PUT /users/123
Content-Type: application/json
{ "name": "Alice Smith", "email": "alice@example.com", "age": 25 }- Replaces the entire resource
- Idempotent, running twice has the same effect
- Requires all fields, even unchanged ones
PATCH: change just this part
PATCH /users/123
Content-Type: application/json
{ "name": "Alice Smith" }- Partially updates a resource
- Only send what changed
- More efficient than PUT for small changes
DELETE: remove this
DELETE /users/123- Removes a resource
- Usually returns
204 No Contenton success - Idempotent, deleting twice is the same as deleting once
POST request to fetch data, or use GET for an operation that deletes something. ChatGPT sometimes puts request bodies on GET requests (which is technically allowed but widely unsupported). Always verify: reads should be GET, creates should be POST, updates should be PUT/PATCH, and deletes should be DELETE. If the AI-generated code uses POST /getUsers or GET /deleteUser/123, that's a red flag.HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. status codes: what happened?
Every response includes a status codeWhat is status code?A three-digit number in an HTTP response that tells the client what happened: 200 means success, 404 means not found, 500 means the server broke., a three-digit number that tells you what happened.
Success codes (2xx)
| Code | Meaning | When to use |
|---|---|---|
| 200 | OK | Request succeeded, here's the data |
| 201 | Created | New resource created successfully |
| 204 | No Content | Success, but nothing to return |
Redirect codes (3xx)
| Code | Meaning | When to use |
|---|---|---|
| 301 | Moved Permanently | Resource moved forever, update your bookmarks |
| 302 | Found | Temporary redirect, check back later |
| 304 | Not Modified | Use your cached version (saves bandwidth) |
Client error codes (4xx)
| Code | Meaning | When to use |
|---|---|---|
| 400 | Bad Request | Invalid syntax or malformed data |
| 401 | Unauthorized | Need to log in first |
| 403 | Forbidden | Logged in, but not allowed to do this |
| 404 | Not Found | Resource doesn't exist |
| 422 | Unprocessable | Validation failed (e.g., email already taken) |
| 429 | Too Many Requests | Rate limit exceeded, slow down |
Server error codes (5xx)
| Code | Meaning | When to use |
|---|---|---|
| 500 | Internal Server Error | Server crashed unexpectedly |
| 502 | Bad Gateway | Server can't reach another server |
| 503 | Service Unavailable | Server temporarily down, try again later |
| 504 | Gateway Timeout | Server took too long to respond |
The difference between 401 and 403 confuses many developers. 401 means "I don't know who you are" (authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. required). 403 means "I know who you are, but you're not allowed" (authorizationWhat is authorization?Checking what an authenticated user is allowed to do, like whether they can delete records or access admin pages. failed). Remember: 401 is "who?", 403 is "no."
Anatomy of a request
An HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. request has three parts:
POST /api/users HTTP/1.1 ← Start line (method + path + version)
Host: example.com ← Headers start here
Content-Type: application/json
Authorization: Bearer token123
Content-Length: 35
← Empty line separates headers from body
{"name": "Alice", "age": 25} ← Body (optional for GET)Start line: Method + Path + HTTP version
Headers: Metadata about the request (format, authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token., preferences)
Body: The actual data (JSONWhat is json?A text format for exchanging data between systems. It uses key-value pairs and arrays, and every programming language can read and write it., form data, file uploads)
Common request headers
| Header | Purpose | Example |
|---|---|---|
| Content-Type | Format of request body | application/json |
| Authorization | Authentication token | Bearer abc123 |
| Accept | What format you want back | application/json |
| User-Agent | Who's making the request | Mozilla/5.0... |
| Cookie | Previously saved data | session=xyz789 |
Anatomy of a response
Responses follow the same pattern:
HTTP/1.1 200 OK ← Status line (version + code + text)
Content-Type: application/json ← Headers
Content-Length: 45
Set-Cookie: session=abc123
← Empty line
{"id": 1, "name": "Alice", "age": 25} ← BodyCommon response headers
| Header | Purpose | Example |
|---|---|---|
| Content-Type | Format of response | application/json |
| Set-Cookie | Save data in browser | session=abc123 |
| Cache-Control | How long to cache | max-age=3600 |
| Location | Where to find resource | /users/123 |
| WWW-Authenticate | How to authenticate | Bearer |
Making HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. requests in JavaScript
// GET request
const response = await fetch('https://api.example.com/users/1');
console.log('Status:', response.status); // 200
const data = await response.json();
// POST request with headers and body
const newUser = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
},
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com'
})
});HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. vs HTTPSWhat is https?HTTP with encryption added, so data traveling between your browser and a server can't be read or tampered with by anyone in between.
| Feature | HTTP | HTTPS |
|---|---|---|
| Encryption | None | SSL/TLS |
| Port | 80 | 443 |
| Security | Anyone can read it | Encrypted and secure |
| Browser indicator | "Not Secure" warning | Lock icon |
Always use HTTPS for: passwords, login forms, personal information, payment details, any authenticated requests.
Viewing HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. in your browser
Open DevTools (F12) then go to the Network tab to see HTTP in action:
- Load a webpage
- Click any request in the list
- See the full request and response headers
- Check the timing breakdown (DNSWhat is dns?The system that translates human-readable domain names like google.com into the numerical IP addresses computers use to find each other., connection, download)
This is invaluable for debugging. You can see exactly what your browser sent and what the server responded with.
Quick reference
| Method | Action | Has body? | Idempotent? |
|---|---|---|---|
| GET | Read data | No | Yes |
| POST | Create resource | Yes | No |
| PUT | Replace resource | Yes | Yes |
| PATCH | Partial update | Yes | No |
| DELETE | Remove resource | No | Yes |