How the web talks: requests and responses

curl · Essentials · 8 min

Every curl command is one of these conversations, so knowing their parts makes every option that follows make sense.

When you open a website, your computer sends a short message to another computer on the internet and gets a reply. The computer that asks is the client. The one that answers is the server: a computer that stays switched on, waiting for questions, like a shop counter waiting for customers.

Usually the client is your browser (Chrome, Safari, Firefox), which turns the reply into a page you can look at. curl is a client you run in the terminal. It sends the same kind of message but simply prints the reply as text, so you see exactly what the server said.

Every message is aimed at a URL, the address you type into a browser. In https://api.example.com/users?limit=5, https:// says how to talk, api.example.com names the server and /users the thing you want on it. The part after ? is the query string: extra settings as name=value pairs, joined by &.

The rules for these messages are called HTTP. A set of rules both sides agree to follow, so they understand each other, is called a protocol: like the fixed layout of a letter, with the address in one place and the signature in another.

HTTPS is HTTP sent through a locked, scrambled connection, so nobody between you and the server can read or change it. The locking layer is called TLS. Almost every site uses HTTPS today; you just write https:// and curl handles the rest.

The client's message is the request. Its first word is the HTTP method, a verb saying what you want: GET means "send me this", POST means "here is some data for you". GET is what curl sends unless you ask for something else.

After that line come headers: one Name: value line each, with extra details such as which formats the client accepts. A request can also carry a body, the data being sent, such as what you typed into a form.

The server's reply is the response. It starts with a status code, a three-digit number saying how it went: 200 means OK, 404 means "not found", 500 means the server itself failed. Then come response headers, such as Content-Type: text/html ("what follows is a "), and then the body: the page, picture or data you asked for.

Example

curl https://example.com          # prints only the response body

# What curl sends for you (the request):
#   GET / HTTP/1.1                <- HTTP method, path, HTTP version
#   Host: example.com             <- headers, one per line
#   User-Agent: curl/8.16.0
#   Accept: */*
#
# What the server sends back (the response):
#   HTTP/1.1 200 OK               <- status code 200 = OK
#   Content-Type: text/html       <- response headers
#   Content-Length: 1256
#
#   <!doctype html> ...           <- the body: the page itself

Remember

  • A client sends a request to a URL; the server answers with a response
  • Request = HTTP method (GET, POST...) + headers + optional body
  • Response = status code (200, 404, 500...) + headers + body
  • HTTPS is HTTP through a locked connection (TLS)

Watch out for

  • Expecting curl to show a page like a browser does: it prints the raw text the server sent, which for a web page is the code a browser would turn into the page.
New words in this lesson (1)
web page
One document on the web, written in HTML and shown in a browser.

Check yourself

  1. In `https://shop.example.com/search?q=lamp&page=2`, which part is the query string?
  2. A server answers with status code 404. What does that tell you?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: curl.se/docs/httpscripting.html

Builds on

Nothing: this is a foundation.

Unlocks