Automation and Artificial Intelligence · Topic 5 of 7
REST Response Codes and Payloads
Interpret HTTP status codes and JSON payloads from Catalyst Center and RESTCONF: 2xx, 4xx and 5xx codes, error bodies, task replies and nested results.
- 5 min
- 2xx means success, 4xx is a client error, and 5xx is a server error.
- 200 is OK, 201 is Created, 202 is Accepted and 204 is No Content.
- 401 is an authentication failure, and 403 is an authorisation failure.
What it is
Every REST reply has a status code and usually a body, and you read both to learn what happened.
The status code is a three digit number that tells you whether the request worked, and if not, whose fault it was. The body, called the payload, holds the data or the error details. Reading them together is how you debug an API call.
How it works
The first digit of the code tells you the class, and the payload gives the detail.
Status code classes
| Class | Meaning |
|---|---|
| 1xx | Informational |
| 2xx | Success |
| 3xx | Redirection |
| 4xx | Client error, so the request was wrong |
| 5xx | Server error, so the server failed |
Codes to know
| Code | Name | Typical meaning |
|---|---|---|
| 200 | OK | The request worked, and the body holds the result |
| 201 | Created | A new resource was created |
| 202 | Accepted | The request was accepted and work continues in the background |
| 204 | No Content | The request worked, and there is no body |
| 400 | Bad Request | The request is malformed, such as invalid JSON |
| 401 | Unauthorized | Authentication is missing or wrong |
| 403 | Forbidden | The caller is known, but not allowed |
| 404 | Not Found | The path or resource does not exist |
| 405 | Method Not Allowed | The method is not supported on that path |
| 409 | Conflict | The request clashes with the current state, such as a duplicate |
| 429 | Too Many Requests | The caller was rate limited |
| 500 | Internal Server Error | The server failed |
| 503 | Service Unavailable | The server is not ready or is overloaded |
A useful habit is to read the first digit. A 4xx means to check your request, and a 5xx means that the problem is on the server.
RESTCONF results
RESTCONF uses status codes in line with the method.
| Method | Success code | Notes |
|---|---|---|
| GET | 200 | The body holds the data |
| POST | 201 | A resource was created, and there is no body |
| PUT | 201 or 204 | Created or replaced |
| PATCH | 204 | Merged, with no body |
| DELETE | 204 | Removed, with no body |
If a GET returns 404, the data path is wrong, or the data is not configured. A 400 often means invalid JSON or a value that does not fit the YANG model. A 401 means a login failure.
An error reply from RESTCONF has a standard body.
{
"ietf-restconf:errors": {
"error": [
{
"error-type": "protocol",
"error-tag": "invalid-value",
"error-message": "The value does not match the model"
}
]
}
}
The error-tag is a short code, and error-message gives a human readable reason. Read these before you try again.
Catalyst Center results
A successful Catalyst Center call returns 200 and a JSON body. A list reply wraps the items under a response key. A call that starts background work returns 202, and the body holds a task identifier.
{
"response": {
"taskId": "TASKIDVALUE",
"url": "/api/v1/task/TASKIDVALUE"
},
"version": "1.0"
}
The client then asks for the task until it reports that it finished, and reads the outcome from there. The first reply only says that the work started, so it does not prove that the change was made.
An error reply from Catalyst Center holds a message that explains the problem. A 401 means the token is missing or has expired, and the fix is to ask for a new token.
Reading a payload in Python
Check the status code first, and then read the body.
resp = requests.get(url, headers=headers, verify=False)
if resp.status_code == 200:
body = resp.json()
for dev in body["response"]:
print(dev["hostname"], dev["reachabilityStatus"])
elif resp.status_code == 401:
print("Login or token problem")
elif resp.status_code == 404:
print("Check the path")
else:
print("Unexpected status", resp.status_code)
The function resp.json() turns the body into a Python dictionary. A 204 reply has no body, so calling it would raise an error.
A method for reading results
- Read the status code. Decide if the problem is the client, the server or none.
- If it is a 4xx, read the error body for the reason.
- If it is a 2xx with a body, find the key that holds the data.
- If it is a 202, find the task and check its result.
- Fix one thing at a time, and try again.
What to remember for the exam
Eight facts cover most questions on this topic.
- 2xx means success, 4xx is a client error, and 5xx is a server error.
- 200 is OK, 201 is Created, 202 is Accepted and 204 is No Content.
- 401 is an authentication failure, and 403 is an authorisation failure.
- 404 means the path or resource was not found.
- RESTCONF PATCH and DELETE normally return 204.
- A RESTCONF error body has
error-type,error-taganderror-message. - Catalyst Center wraps its results in a
responsekey. - A 202 reply means the work is still running, so check the task.
When a question shows a code, look at the first digit first. It tells you whether to look at the request or at the server.
Common mistakes
These four mistakes appear often.
Treating 202 as finished. The work may still fail, so check the task.
Mixing up 401 and 403. A 401 is about who you are, and a 403 is about what you may do.
Reading a body from a 204. There is no body to parse.
Ignoring the error message. The body usually says exactly what was wrong.
A 200 reply does not always mean the change you wanted was made. Read the payload to confirm.
Check yourself
Work through these three questions.
What does a 202 response from Catalyst Center tell you?
The request was accepted and the work continues in the background, so you check the task for the result.
What is the difference between 401 and 403?
A 401 means authentication is missing or wrong, and a 403 means the caller is known but not allowed.
Which status code does RESTCONF normally return for a successful DELETE?
204 No Content.
Topic 5 of 7 in Automation and Artificial Intelligence
Test yourself on Automation and Artificial Intelligence
The full Cisco CCNP 350-401 ENCOR question bank has 854 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.