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
In a nutshell
  • 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.

Requestmethod, path, headerssent toServerAPIreturnsStatus code3 digitsandPayloadJSON body

How it works

The first digit of the code tells you the class, and the payload gives the detail.

Status code classes

ClassMeaning
1xxInformational
2xxSuccess
3xxRedirection
4xxClient error, so the request was wrong
5xxServer error, so the server failed

Codes to know

CodeNameTypical meaning
200OKThe request worked, and the body holds the result
201CreatedA new resource was created
202AcceptedThe request was accepted and work continues in the background
204No ContentThe request worked, and there is no body
400Bad RequestThe request is malformed, such as invalid JSON
401UnauthorizedAuthentication is missing or wrong
403ForbiddenThe caller is known, but not allowed
404Not FoundThe path or resource does not exist
405Method Not AllowedThe method is not supported on that path
409ConflictThe request clashes with the current state, such as a duplicate
429Too Many RequestsThe caller was rate limited
500Internal Server ErrorThe server failed
503Service UnavailableThe 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.

MethodSuccess codeNotes
GET200The body holds the data
POST201A resource was created, and there is no body
PUT201 or 204Created or replaced
PATCH204Merged, with no body
DELETE204Removed, 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.

Clientsends a change202 Accepted and task idControllerworks in backgroundclient checks taskTask resultsuccess or failure

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

  1. Read the status code. Decide if the problem is the client, the server or none.
  2. If it is a 4xx, read the error body for the reason.
  3. If it is a 2xx with a body, find the key that holds the data.
  4. If it is a 202, find the task and check its result.
  5. 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-tag and error-message.
  • Catalyst Center wraps its results in a response key.
  • A 202 reply means the work is still running, so check the task.
Exam tip

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.

Watch out

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.