Instiq
Chapter 2 · Understanding and Using APIs·v1.0.0·Updated 7/20/2026·~16 min

What's changed: Initial version

2.1Constructing REST API requests

Key points

Covers assembling a REST API request that accomplishes a task from the API docs: the HTTP method (GET/POST/PUT/PATCH/DELETE) and endpoint (URL), headers such as Content-Type/Accept/Authorization, query parameters, and the JSON request body, together with the Basic authentication, API key, and Bearer token (OAuth) authentication schemes, framed as the diagnosis of "does my request match what the docs demand?"

Working with APIs is not about memorizing a list of methods; it is about reading the API docs and assembling the correct HTTP request that accomplishes your task. Even for the same goal of "get the list of devices," the endpoint URL, required headers, authentication scheme, and needed parameters differ per API, and getting any one of them wrong makes the request fail. This section nails the parts of a request and the common authentication schemes (Basic, API key, Bearer token), building the eye to pinpoint where your request diverges from what the docs require.

2.1.1Anatomy of a request

  • The HTTP method expresses the kind of operation: GET (retrieve, no side effect), POST (create), PUT (replace the whole resource), PATCH (partial update), DELETE (remove). If the docs say "create a new resource" you use POST; if they say "change only some fields of an existing one" you use PATCH—choose the method from what you want to do.
  • The endpoint (URL) locates the resource, with the path tail or query narrowing the target, e.g., GET https://api.example.com/v1/devices?limit=50. Query parameters (?limit=50&offset=0) handle filtering/paging, while the request body (usually JSON) carries the data payload for POST/PUT/PATCH. GET normally has no body.
  • The headers carry request metadata: Content-Type: application/json declares the format of the body you send, Accept: application/json states the response format you want, and Authorization carries credentials. Forgetting Content-Type when sending a JSON body often yields 400 Bad Request or 415, because the server cannot interpret the payload.

2.1.2Authentication schemes

  • Basic authentication sends username:password base64-encoded as Authorization: Basic <base64>. It is simple, but base64 is encoding, not encryption, and is trivially reversible—so use it only over HTTPS (TLS). Do not mistake base64 for an "encrypted, safe" value.
  • An API key is an issued secret string placed where the docs specify—either a header (e.g., X-Api-Key: <key> or Authorization) or a query parameter (e.g., ?api_key=<key>). The crux is to follow the exact location and name the docs prescribe; putting a header-required key in the query, or misspelling the key name, are typical failures.
  • A Bearer token is obtained by authenticating first (OAuth being the classic flow), then sent on each call as Authorization: Bearer <token>. Tokens have an expiry; once expired you get 401 and must re-obtain (refresh) one. Many Cisco DevNet APIs (e.g., Catalyst Center) use this token model, where you hit a token-issuing endpoint first, then call the actual API.
Exam point

Most-tested: choose the method from the goal (create = POST, partial update = PATCH, replace = PUT); a JSON body needs Content-Type: application/json; Basic uses base64 (not encryption, HTTPS required); an API key follows the exact location (header/query) and name the docs specify; a Bearer token is sent as Authorization: Bearer <token> and has an expiry. Read each question as "given these docs, what do I fix so the request succeeds?"

Suppose you are writing a script to "get the list of users" from a SaaS API and run this curl, but it returns 401 Unauthorized: curl -X GET 'https://api.example.com/v1/users' -H 'Authorization: Basic YWxpY2U6c2VjcmV0'. Jumping to "let me re-issue a token" is premature; the correct first step is to re-read which authentication scheme the docs require. The docs say: "Authentication: Bearer token. Obtain a token from POST /v1/auth/token, then send Authorization: Bearer <access_token>." In other words this API does not accept Basic, and you have picked the wrong scheme entirely. The base64 YWxpY2U6c2VjcmV0 merely encodes alice:secret—it is neither encryption nor a valid token. The correct flow is: (1) POST /v1/auth/token with your credentials to obtain an access token, then (2) call the endpoint with -H 'Authorization: Bearer <access_token>'. If the scheme is right (you are sending Bearer) yet you still get 401, the next suspect is an expired token—refresh and resend. But if authentication succeeds and you instead get 403 Forbidden, that is different: "we know who you are, but you lack permission"—a matter of the token's scope/role. The lesson is that even from a single 401, isolating in the order docs requirement (scheme) -> what you sent (scheme/expiry) -> scope reaches the cause by the shortest path without blind retries. Authentication is not "attach anything and it works"—it is matching the exact scheme, location, and name the docs prescribe, to the letter.

SchemeHow it is sentCharacteristicsTypical failure
Basic auth`Authorization: Basic <base64(user:pass)>`Simple; base64 is not encryption; requires HTTPSMistaking base64 for safe; plaintext exposure over HTTP
API keyHeader (e.g., `X-Api-Key`) or query `?api_key=`Follow the docs' exact location and nameWrong location (header/query) or name -> 401
Bearer token`Authorization: Bearer <token>`Obtained by authenticating first (OAuth); has expiryExpired -> 401; insufficient scope -> 403
Warning

Trap: "When you get 401, just re-issue the token first" is wrong—for 401, first confirm whether the scheme the docs require matches the scheme you are sending (e.g., are you sending Basic when Bearer is mandatory?); only when the scheme is correct and the token is expired do you refresh. Also wrong: "Basic auth's base64 is encryption, so it is safe"—base64 is mere encoding, trivially reversible, and the actual protection comes from the underlying HTTPS (TLS).

HTTP methods, headers, and auth schemes (Basic/API key/Bearer).
Assembling a request to match the docs

2.1.3Section summary

  • Assemble a request from method (the goal) + endpoint + headers + (if needed) query/JSON body, and add Content-Type: application/json to a JSON body
  • Authentication is Basic (base64, HTTPS required) / API key (docs' exact location and name) / Bearer token (Authorization: Bearer, has expiry); match the docs to the letter
  • Isolate a 401 in the order "scheme mismatch -> expiry -> (if 403) insufficient scope," rather than jumping to blind re-issuance

Sign in to track progress — Log in.

Quick check

(just a quick review)

Q1. The API docs state: "Authentication: send the API key in the request header X-Api-Key." You run `curl 'https://api.example.com/v1/devices?api_key=ABC123'` but get `401 Unauthorized`. Which fix is most appropriate?

Q2. The docs define authentication as a Bearer token (obtained via `POST /auth/token`, sent as `Authorization: Bearer <token>`). You call the correct endpoint and method, but although you send a JSON body the server cannot parse it and returns `400 Bad Request`. What should you check first?

Q3. The docs for a Cisco-style API state: "first send credentials to `POST /dna/system/api/v1/auth/token` to obtain a token, then attach that token to every subsequent request." Which understanding of this authentication model is most appropriate?

Check your understandingPractice questions for Chapter 2: Understanding and Using APIs