Understanding and Using APIs · Topic 1 of 7
Constructing a REST API Request
Learn the parts of a REST API request, how HTTP methods map to create, read, update and delete, and how to build a request from API documentation.
- 4 min
- A request has a method, a URL, headers and an optional body.
- GET reads, POST creates, PUT replaces, PATCH changes part, and DELETE removes.
- Path parameters identify one item. Query parameters filter a list.
What it is
A REST API request is an HTTP message that asks a server to read or change a resource.
REST treats everything as a resource with its own address. You act on a resource by sending a method such as GET or POST to that address.
To build a correct request from documentation, you pick the method, build the address, add headers and, when needed, add a body.
How it works
Every request has the same four parts, and the documentation tells you the value for each.
Parts of a URL
A URL has several parts.
| Part | Example | Meaning |
|---|---|---|
| Scheme | https | The protocol, which is HTTPS for secure APIs |
| Host | api.example.test | The server name |
| Port | 8443 | Optional. It defaults to 443 for HTTPS |
| Path | /v1/devices/42 | The resource |
| Query string | ?limit=10 | Optional filters, written as name and value pairs |
Path parts that identify one item, such as the device ID 42 above, are called path parameters. Query parameters filter or page a list and are joined with an ampersand sign.
HTTP methods and CRUD
| Method | CRUD action | Typical use |
|---|---|---|
| GET | Read | Retrieve a resource or a list |
| POST | Create | Add a new resource |
| PUT | Update | Replace a resource completely |
| PATCH | Update | Change part of a resource |
| DELETE | Delete | Remove a resource |
GET and DELETE normally have no body. POST, PUT and PATCH usually carry a body that describes the data.
A method is safe if it only reads, which is true of GET. A method is idempotent if repeating it gives the same final result. GET, PUT and DELETE are idempotent, but POST is not, because each call can create another resource.
Headers
Headers carry details about the request.
Authorizationholds credentials, as covered on the authentication page.Content-Typestates the format of the body that you send.Acceptstates the format that you want back.
Reading the documentation
API documentation lists each operation with its method, path, parameters and an example. Suppose it says this for creating a space in a messaging service:
POST /v1/rooms
Header: Authorization: Bearer ACCESS_TOKEN
Header: Content-Type: application/json
Body field: title (string, required)
Response: 200 with the new room as JSON
You turn that into a raw HTTP request.
POST /v1/rooms HTTP/1.1
Host: api.example.test
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json
{"title": "Network Ops"}
The first line holds the method, the path and the HTTP version. The Host header names the server. A blank line separates the headers from the body.
The same request in Python
import requests
url = BASE_URL + "/v1/rooms"
headers = {
"Authorization": "Bearer ACCESS_TOKEN",
"Content-Type": "application/json",
}
payload = {"title": "Network Ops"}
response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(response.json())
Here BASE_URL stands for the scheme and host given in the documentation. The json= argument converts the dictionary to JSON for you.
A GET with query parameters
params = {"limit": 10, "type": "group"}
response = requests.get(BASE_URL + "/v1/rooms", headers=headers, params=params)
The library builds the query string from the dictionary. The path ends up as /v1/rooms?limit=10&type=group.
Checking your request against the documentation
- The method matches the action, for example POST to create.
- The path is complete, and every path parameter is filled in.
- Required headers and body fields are present.
- The body is valid JSON and the Content-Type says so.
When a task says to add something, think POST. When it says to get or list something, think GET. Updates use PUT or PATCH and removals use DELETE.
What to remember for the exam
Seven facts cover this topic.
- A request has a method, a URL, headers and an optional body.
- GET reads, POST creates, PUT replaces, PATCH changes part, and DELETE removes.
- Path parameters identify one item. Query parameters filter a list.
Content-Typedescribes the body that you send.Acceptdescribes what you want back.- GET, PUT and DELETE are idempotent. POST is not.
- A blank line separates the headers from the body in a raw request.
- In
requests,json=sends a dictionary as a JSON body andparams=builds the query string.
Common mistakes
Four mistakes are typical.
Using GET to create something. Creating needs POST, and the data goes in the body.
Forgetting the Content-Type header. The server may not understand a JSON body without it.
Putting a secret in the query string. URLs are logged. Credentials belong in a header.
Mixing up PUT and PATCH. PUT replaces the whole resource, while PATCH changes only the fields that you send.
Sending a request twice can have different effects. A repeated POST can create two resources.
Check yourself
Work through these three questions.
Which HTTP method creates a new resource, and where does the data go?
POST creates a resource, and the data goes in the request body.
What is the difference between a path parameter and a query parameter?
A path parameter identifies one item in the path. A query parameter filters or pages a list after the question mark.
Why is POST not idempotent?
Repeating a POST can create another resource each time, so the final result changes.
Topic 1 of 7 in Understanding and Using APIs
Test yourself on Understanding and Using APIs
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.