Automation and APIs
REST API
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.
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:
- Authenticate. A request to
/api/routeswithout a token gets 401 and aWWW-Authenticate: Bearerheader. The client logs in once withPOST /api/loginand a JSON body, gets a token back, and sends it on every request asAuthorization: Bearer <token>. See API authentication. - Read a resource.
GET /api/routesreturns a JSON list,[]while the router has no static routes. Each request is stateless: it carries its own token and everything the server needs. - Create.
POST /api/routeswith a JSON body creates a member of the collection. The reply is 201 Created with aLocationheader naming the new resource's URL,/api/routes/10.0.9.0/24. - Change.
PUT /api/routes/10.0.9.0/24says what the whole resource should be: 200 when it was there, 201 when the PUT made it.PATCHchanges only the fields sent. - Delete.
DELETE /api/routes/10.0.9.0/24removes 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.
Read next
- JSON The text format APIs exchange data in: objects of key: value pairs, arrays, strings, numbers, true, false and null.
- API authentication How a program proves who it is to an API: a password on every request (Basic), a token it got once (Bearer), or a key.
- Network controller A central system that holds the network's intended state and configures the devices for you, behind an API.
- HTTP The web's request and response protocol: plain text over TCP unless wrapped in TLS.
- NETCONF and YANG A device's config as data: YANG models say what it may hold, and NETCONF reads and changes it as XML over SSH.
- Running and startup config What a device does now against what it loads when it starts: unsaved changes vanish at the next reload.
- Idempotence Doing it again changes nothing: a run that describes a state leaves a device already in that state alone.