ApiSkills

Foundations · 11 min

HTTP: requests, responses & headers

The request/response exchange behind every API: methods, status codes, MIME types, and the headers that matter for automation.

HTTP is the conversation every API has: your program sends a request, the server sends back a response. Both share the same three-part shape — a start line, headers, and an optional body. Learn that shape once and you can read and debug any API call, in any tool.

The request has three parts

  • Start line — the method + the path + the HTTP version: GET /v1/orders HTTP/1.1.
  • Headers — metadata about the request (who you are, what you are sending).
  • Body — the payload, only for methods that send data (POST, PUT, PATCH).

The methods (verbs)

The method states your intent. The server decides what actually happens, but the conventions are near-universal:

MethodIntentBody?Repeat-safe?
GETRead data. The most common by far.NoYes — never changes data
POSTCreate something, or trigger an action.YesNo — sending twice creates two
PUTReplace a resource entirely.YesYes — same result if repeated
PATCHUpdate part of a resource.YesUsually
DELETERemove a resource.SometimesYes
HEAD / OPTIONSMetadata / capability checks — headers only, no body.NoYes

"Repeat-safe" (idempotent) means doing it again has the same effect as doing it once — which matters when a network retry might resend a request. A retried GET or PUT is safe; a retried POST can double-charge or create a duplicate.

The response and its status code

The server answers with a status line (a three-digit code), its own headers, and usually a body (the JSON you want). The first digit of the code tells you what happened:

RangeMeaningCommon ones
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirect — go somewhere else301 Moved, 302 Found, 304 Not Modified
4xxYou made a mistake400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests
5xxThe server failed500 Internal Error, 502 Bad Gateway, 503 Unavailable, 504 Timeout

Tip · The 4xx vs 5xx split is the first thing to check when something fails: 4xx means fix your request (wrong key, bad URL, missing field); 5xx means the server is unhappy — back off and retry.

Content-Type and MIME types

A MIME type (or "media type") names the format of a body so the receiver knows how to read it. It travels in the Content-Type header, and the same values describe both what you send and what you get back.

MIME typeWhat it is
application/jsonJSON — the default for almost every modern API.
application/xml, text/xmlXML data.
application/x-www-form-urlencodedForm fields as a=1&b=2, like an HTML form.
multipart/form-dataFile uploads and mixed form fields.
text/plain, text/htmlPlain text, or an HTML page.
application/octet-streamRaw bytes — a generic file download.

Two headers work together: Content-Type describes the body you are sending; Accept tells the server which format you would like back. Set Content-Type wrong and the server may reject or misread your body.

Headers worth knowing for automation

Headers are key: value metadata lines. A handful come up constantly when you build integrations — these are the ones to memorise.

HeaderDirectionWhy it matters
AuthorizationrequestCarries your credentials — Bearer <token> or Basic <...>. The #1 header in API work.
Content-TypebothThe format of the body being sent.
AcceptrequestThe format you want back, e.g. application/json.
User-AgentrequestIdentifies your client. Some APIs reject requests that do not send one.
Content-LengthbothSize of the body in bytes.
LocationresponseWhere a 3xx redirect — or a 201 Created — points to.
Retry-AfterresponseOn a 429/503, how many seconds to wait before retrying. Respect it.
Cache-Control / ETagresponseWhether and how you may cache; ETag enables cheap "has it changed?" checks.
X-RateLimit-RemainingresponseHow many requests you have left in the window — watch it to avoid 429s.
Set-CookieresponseSession cookies; relevant for login-based sites, less so for token APIs.

Tip · When an automation "works in the browser but not in my tool", the difference is almost always headers — a missing Authorization, the wrong Content-Type, or an absent User-Agent/Accept. Log the exact request headers and compare the two.

These are universal HTTP concepts: they work the same whether you call the API from a browser, a script, a no-code tool, or an AI agent. Every later lesson is just these fundamentals applied.