Course:Internet & Tools/
Lesson

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 Content on success
  • Idempotent, deleting twice is the same as deleting once
AI pitfall
AI tools regularly use the wrong HTTP methods. Copilot might generate a 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.
02

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)

CodeMeaningWhen to use
200OKRequest succeeded, here's the data
201CreatedNew resource created successfully
204No ContentSuccess, but nothing to return

Redirect codes (3xx)

CodeMeaningWhen to use
301Moved PermanentlyResource moved forever, update your bookmarks
302FoundTemporary redirect, check back later
304Not ModifiedUse your cached version (saves bandwidth)

Client error codes (4xx)

CodeMeaningWhen to use
400Bad RequestInvalid syntax or malformed data
401UnauthorizedNeed to log in first
403ForbiddenLogged in, but not allowed to do this
404Not FoundResource doesn't exist
422UnprocessableValidation failed (e.g., email already taken)
429Too Many RequestsRate limit exceeded, slow down

Server error codes (5xx)

CodeMeaningWhen to use
500Internal Server ErrorServer crashed unexpectedly
502Bad GatewayServer can't reach another server
503Service UnavailableServer temporarily down, try again later
504Gateway TimeoutServer 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."

03

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

HeaderPurposeExample
Content-TypeFormat of request bodyapplication/json
AuthorizationAuthentication tokenBearer abc123
AcceptWhat format you want backapplication/json
User-AgentWho's making the requestMozilla/5.0...
CookiePreviously saved datasession=xyz789
04

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}  ← Body

Common response headers

HeaderPurposeExample
Content-TypeFormat of responseapplication/json
Set-CookieSave data in browsersession=abc123
Cache-ControlHow long to cachemax-age=3600
LocationWhere to find resource/users/123
WWW-AuthenticateHow to authenticateBearer
05

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'
  })
});
06

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.

FeatureHTTPHTTPS
EncryptionNoneSSL/TLS
Port80443
SecurityAnyone can read itEncrypted and secure
Browser indicator"Not Secure" warningLock icon

Always use HTTPS for: passwords, login forms, personal information, payment details, any authenticated requests.

07

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:

  1. Load a webpage
  2. Click any request in the list
  3. See the full request and response headers
  4. 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.

08

Quick reference

MethodActionHas body?Idempotent?
GETRead dataNoYes
POSTCreate resourceYesNo
PUTReplace resourceYesYes
PATCHPartial updateYesNo
DELETERemove resourceNoYes