Concepts / Automation and APIs

Automation and APIs

REST API

Also called API, CRUD, GET, POST, PUT, PATCH, DELETE, resource, endpoint, status code, RESTCONF, idempotent method

An HTTP interface where every thing is a URL and the methods are the verbs: GET reads, POST creates, PUT and PATCH change, DELETE removes.

The URL names the thing, the method says what to do
The URL names the thing, the method says what to do

The same URL takes GET to read, PUT to replace and DELETE to remove; POST to the collection creates a new member. Every reply starts with a status code that says how it went.

A REST API is an HTTP interface where every resource has a URL (/api/routes/1) and the method says what to do: POST creates, GET reads, PUT replaces, PATCH changes some fields, DELETE removes. Each request is stateless and carries its own credentials. Status codes: 2xx worked, 4xx the request was wrong, 5xx the server failed. GET, PUT and DELETE are idempotent; POST is not: repeated, it creates a second copy.

How a REST API works, step by step

A REST API is an HTTP interface where every thing is a URL (a resource) and the methods are the verbs. Follow the network APIs lab, where Maya's machine desk drives the router hq through the API it serves over HTTPS:

  1. Authenticate. A request to /api/routes without a token gets 401 and a WWW-Authenticate: Bearer header. The client logs in once with POST /api/login and a JSON body, gets a token back, and sends it on every request as Authorization: Bearer <token>. See API authentication.
  2. Read a resource. GET /api/routes returns a JSON list, [] while the router has no static routes. Each request is stateless: it carries its own token and everything the server needs.
  3. Create. POST /api/routes with a JSON body creates a member of the collection. The reply is 201 Created with a Location header naming the new resource's URL, /api/routes/10.0.9.0/24.
  4. Change. PUT /api/routes/10.0.9.0/24 says what the whole resource should be: 200 when it was there, 201 when the PUT made it. PATCH changes only the fields sent.
  5. Delete. DELETE /api/routes/10.0.9.0/24 removes it and returns 204 No Content; repeating it returns 404.

The status code says how it went: 2xx success, 400 bad request, 401 not authenticated, 403 not allowed, 404 not found, 409 conflict (a POST of something that's already there), 5xx the server failed. GET, PUT and DELETE are idempotent (repeating changes nothing more); POST is not. RESTCONF is the standardised REST API for network devices, built on YANG models, and a network controller exposes one like this.

What it looks like on a Linux machine

A request with no token is refused; the token from the login lets it through (headers and JSON shortened):

$ curl -si https://hq/api/routes
HTTP/1.1 401 UNAUTHORIZED
WWW-Authenticate: Bearer realm="hq"
{"error": "no token: log in at POST /api/login, then send the header Authorization: Bearer <token>"}
$ curl -s https://hq/api/routes -H 'Authorization: Bearer 7f3e9a2c51d84b06'
[]

Creating a route returns 201 with a Location header, and the router forwards to the new network at once:

$ curl -si https://hq/api/routes -H 'Authorization: Bearer 7f3e9a2c51d84b06' \
    -H 'Content-Type: application/json' -d '{"destination": "10.0.9.0/24", "via": "10.0.2.3"}'
HTTP/1.1 201 CREATED
Location: /api/routes/10.0.9.0/24
{"destination": "10.0.9.0/24", "via": "10.0.2.3"}

jq reads the JSON reply, so curl and jq together are a whole toolkit:

$ curl -s https://hq/api/routes -H 'Authorization: Bearer 7f3e9a2c51d84b06' | jq -r '.[] | .destination + " via " + .via'
10.0.9.0/24 via 10.0.2.3

A /33 mask gets 400 with the reason; a read-only token deleting gets 403, grafana may read but not change anything; and DELETE returns HTTP/1.1 204 NO CONTENT with an empty body.

What goes wrong, and how you see it

401 you did not expect. The server returns 401 UNAUTHORIZED with WWW-Authenticate: Bearer. Either no Authorization header was sent, or the token isn't one the server knows: a typo, an expired token, or one taken away. Log in again for a fresh one.

403 not 401. The request authenticated but is not allowed: in the lab, Grafana's read-only token sending DELETE gets 403 grafana may read but not change anything. The fix is a credential with write permission, not a retry.

400 from a bad body. A destination like 10.0.8.0/33 returns 400 and a message saying why. Forgetting Content-Type: application/json makes many servers read the body as empty. Send valid JSON and the header.

409 on a second POST. Something with that key is already there, though not necessarily what you sent. GET it and compare, or PUT the state you want.

PUT wiped a field. PUT replaces the whole resource, so a field left out reverts to its default or the request is refused. Use PATCH to change one field, PUT only to replace everything.

Questions people ask

What is the difference between PUT and PATCH? PUT replaces the entire resource, so the body must carry every field or the missing ones reset; PATCH merges only the fields you send. Both are used to change an existing resource; POST creates a new one.

What does idempotent mean? Repeating the request has no extra effect. GET, PUT and DELETE are idempotent: deleting twice still leaves the resource gone (the second returns 404). POST is not, so each POST creates another member, or gets a 409 when the server refuses a duplicate.

Is REST the same as JSON? No. REST is the style (resources as URLs, HTTP verbs, stateless requests); JSON is just the common body format. RESTCONF serves the same data as JSON or XML, picked with the Accept header.

What are NETCONF and YANG? YANG is the schema language for network config: a model such as ietf-interfaces says what can be set and which values are allowed, so a router refuses a /33 before storing it. NETCONF is the protocol that reads and writes that data: XML over SSH on port 830, with a candidate datastore for drafts, a <commit> that makes them live, and a confirmed commit that undoes itself unless confirmed. RESTCONF serves the same YANG data over HTTP. The NETCONF and YANG lab configures a router's card this way.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab