Skip to content

HTTP basics ​

HTTP is the set of rules that requests and responses follow on the web. Once you can read one request and one response, you can read them all.

A request has four parts ​

http
POST https://api.example.com/todos?notify=true
Content-Type: application/json
Authorization: Bearer abc123

{ "title": "Buy milk" }
PartIn the exampleWhat it is
MethodPOSTWhat you want done
URLhttps://api.example.com/todos?notify=trueWhere to send it
HeadersContent-Type, AuthorizationExtra information about the request
Body{ "title": "Buy milk" }The data you are sending. Many requests have none

Methods: what you want done ​

MethodMeaningHas a body?Example
GETRead somethingUsually notGet the list of todos
POSTCreate something, or run an actionYesAdd a todo, sign in
PUTReplace something completelyYesOverwrite todo 5
PATCHChange part of somethingYesMark todo 5 as done
DELETERemove somethingUsually notDelete todo 5
HEADLike GET, but only the headers come backNoCheck a file exists
OPTIONSAsk what the server allowsNoUsed by browsers before some requests

Two properties are worth knowing because tests rely on them:

  • Safe methods (GET, HEAD, OPTIONS) only read. Sending one should never change anything.
  • Idempotent methods (GET, PUT, DELETE) give the same result whether you send them once or five times. POST is not: send it twice and you may create two todos.

The URL: where it goes ​

text
https://api.example.com:443/todos/5?fields=title&done=false
└─┬─┘   └──────┬──────┘ └┬┘└──┬───┘ └──────────┬──────────┘
scheme       host      port  path          query string
  • Scheme: https is encrypted, http is not. Use https for anything real.
  • Host: which server. localhost means "this computer".
  • Port: which door on that server. Left out, it is 443 for https and 80 for http. A mock on your machine might be at http://localhost:4010.
  • Path: which endpoint. /todos/5 usually means "todo number 5". That 5 is a path parameter.
  • Query string: options after the ?, as name=value pairs joined by &. These are query parameters.

A response has three parts ​

http
HTTP/1.1 201 Created
Content-Type: application/json
Location: /todos/101

{ "id": 101, "title": "Buy milk", "done": false }

A status code and its text, headers, and usually a body.

Status codes: how it went ​

The first digit tells you the kind of answer.

RangeMeaningWho should act
2xxIt workedNobody
3xxLook somewhere else (a redirect)The client follows it
4xxThe request was wrongThe client: fix the request
5xxThe server failedThe server's owner

The ones you will see every day:

CodeNameTypical meaning
200OKSuccess, with data in the body
201CreatedSuccess, and something new now exists
204No ContentSuccess, and there is nothing to send back
301 / 302MovedThe thing is at another URL
304Not ModifiedYour saved copy is still good
400Bad RequestThe server could not understand the request
401UnauthorizedYou did not say who you are, or the credentials are wrong
403ForbiddenThe server knows who you are, and you are not allowed
404Not FoundNothing exists at that URL
405Method Not AllowedThe URL exists, but not for that method
409ConflictThe request clashes with the current state
422Unprocessable ContentThe request was understood but a value is not acceptable
429Too Many RequestsYou are sending too fast. Slow down
500Internal Server ErrorThe server hit a bug
502 / 503 / 504Bad Gateway / Unavailable / TimeoutThe server, or something in front of it, is down or overloaded

401 or 403?

401 means "who are you?" 403 means "I know who you are, and the answer is no." A good test suite checks that an API uses the right one.

Headers: the labels on the parcel ​

Headers are Name: value lines. Header names are not case-sensitive.

HeaderSent byWhat it says
Content-TypeBothThe format of the body, such as application/json
AcceptClientThe formats the client would like back
AuthorizationClientWho the client is: a token or encoded credentials
User-AgentClientWhich program is asking
Cookie / Set-CookieClient / serverSmall values the server asks the client to keep and send back
LocationServerWhere the new or moved thing is
Cache-ControlBothWhether the answer may be saved and reused
Retry-AfterServerHow long to wait before trying again

The body: the data ​

A body can be any format. The Content-Type header says which.

Content-TypeLooks likeUsed for
application/json{"title": "Buy milk"}Most modern APIs
application/x-www-form-urlencodedtitle=Buy+milk&done=falseSimple web forms
multipart/form-dataSeveral parts, some of them filesUploads
text/plainHelloPlain text
application/xml<todo><title>Buy milk</title></todo>Older and enterprise APIs

Where each part goes in ApiWorkbench ​

Part of the requestWhere you set it
Method and URLThe bar at the top of the request
Query parameters and path variablesThe Params tab
HeadersThe Headers tab
BodyThe Body tab
CredentialsThe Auth tab
Part of the responseWhere you read it
Status, time and sizeThe bar above the response
BodyThe Pretty and Raw tabs
HeadersThe Headers tab

See Build a request and Read the response for the details.

What you learned ​

  • A request is a method, a URL, headers and an optional body.
  • A response is a status code, headers and an optional body.
  • 2xx worked, 4xx is the client's mistake, 5xx is the server's.

Next steps ​