REST APIs
Synchronized Transcript
135 cues indexed • Click dialogue or words to jumpWelcome to Jeremy’s IT Lab. This is a free, complete course for the CCNA. If you like these videos, please subscribe to follow along with the series.
Also, please like and leave a comment, and share the video to help spread this free series of videos. Thanks for your help. In this video we will look at REST APIs.
APIs are used to allow programs to exchange information with each other. They are important for network automation because they allow our programs to easily interact with network devices and controllers.
REST APIs specifically are commonly used for the Northbound interface in SDN architecture. I’ve already briefly introduced REST APIs and SDN, but in this video we’ll look at REST APIs in greater depth, and then in the next we’ll do the same for SDN.
This video will cover exam topic 6.5, which states that you must be able to describe characteristics of REST-based APIs. So, as with all of the exam topics in this section, Cisco doesn’t expect you to master the use of REST APIs, they just expect you to have a foundational understanding.
Here’s what we’ll cover in this video. First I’ll briefly review APIs. Then I’ll introduce CRUD operations and HTTP.
CRUD stands for Create, Read, Update, and Delete, and it refers to the different actions we perform when using REST APIs. HTTP also uses CRUD operations, and REST APIs often use HTTP as their communication protocol over the network, so it’s important
to understand HTTP. Then I’ll outline the characteristics of REST APIs, and finally we’ll use Cisco DevNet to try out a couple basic REST API Calls, and it’s free to use so you can follow along too.
Make sure to watch until the end of the video for a bonus practice question from Boson Software’s ExSim, the best practice exams for the CCNA. So, let’s quickly review what an API is.
An API, application programming interface, is a software interface that allows two applications to communicate with each other. They are essential not just for network automation, but for all kinds of applications.
If you want other applications to be able to access data from your application, you should design an API that allows them to access it. Here’s that basic diagram of SDN architecture again, which we’ll cover in greater detail in the next video.
In SDN architecture, APIs are used to communicate between apps and the SDN controller via the northbound interface, and between the SDN controller and the network devices via the southbound interface.
The NBI typically uses REST APIs, the topic of this video. Some other examples of APIs that can be used to configure network devices via the southbound interface are NETCONF and RESTCONF, but we won’t cover those in this video.
You’ll have to learn more about those for the CCNP and beyond, however. So, let’s take a look at CRUD. CRUD stands for Create, Read, Update, Delete, and it refers to the operations we perform using REST APIs.
But this CRUD framework isn’t just used by REST APIs, and I’ll introduce it using HTTP in the next slide. First let’s look at the definition of each of those operations.
Create operations are used to create new variables and set their initial values. For example, you could create a variable “ip_address” and set the value to “10.1.1.1”.
Read operations are used to retrieve the value of a variable, for example you could then ask ‘What is the value of variable “ip_address”?
Update operations are used to change the value of a variable, for example you could change the value of the variable “ip_address” to “10.2.3.4”.
And finally delete operations are used to delete variables, so you could delete the variable “ip_address”. So, those are the CRUD operations, now let’s look at HTTP.
First, why should I talk about HTTP, even though this video is about REST? First, HTTP uses ‘verbs’ or ‘methods’ that map to these CRUD operations, and these CRUD operations match the actions we typically want to perform with an application.
When an HTTP client sends a request to an HTTP server, it includes a verb indicating what the client wants to do. For example, if the client is trying to access a web page on a server, it will use the GET verb to retrieve that web page, to get it, from the server.
And for this reason, among others, REST APIs typically use HTTP as their application-layer protocol to communicate over the network. So, let’s compare these CRUD operations to HTTP verbs.
Here is a simple chart. The HTTP verb POST is used to create a new variable. The equivalent of a read operation is GET.
There are two main options for the update operation, PUT and PATCH, and the verb for the delete operation is simply DELETE. You don’t have to know the exact details of these verbs, but definitely know which ones are used for which CRUD operation.
The flashcards will help with remembering them. There are other HTTP verbs too, and you can see them in this guide from Mozilla if you want to learn more.
Let’s look more at how those verbs are used. When an HTTP client, or a REST client using HTTP, sends a request to a server, it includes some information such as an HTTP verb, which I just introduced and a URI, uniform resource identifier, indicating
the resource it is trying to access. So, here we have a server storing data as variables, with a URI for each resource.
The client sends a GET message, for example, trying to retrieve the resource with URI A. The server would then send a response, but I’ll get into the details of the response soon.
Here’s an example of a URI, and I’ll use this URI later when demonstrating a REST API call on Cisco DevNet.
Note the three main sections. First is the scheme, which is basically the protocol. In this case HTTP, or more specifically HTTPS, HTTP Secure, is the protocol.
Then there is the authority. This is basically the address, or hostname, you are sending the request to. After that is the path, this indicates the resource you want to access.
Now, the Verb and URI are just part of the HTTP request, let’s look at some more of an HTTP request. The HTTP request can include additional headers which pass additional information to the server.
There is a great list at this page here from Mozilla. Here’s an example of an HTTP request message. On the outside there is an IP header and a TCP header, then the HTTP verb and URI, followed by some additional headers and data.
An example header would be the Accept header, which informs the server about the type or types of data that can be sent back to the client. The client might say I accept JSON data or XML data, for example.
There’s another list of standard HTTP headers here on Wikipedia, and there are examples for each header so you can see what kind of information is included.
Now, before I continue I want to clarify once more why I’m spending so much time talking about HTTP in a video that’s supposed to be about REST APIs.
It’s because when a REST client makes an API call, a request to a REST server, it will send an HTTP request like the one above. So, you have to understand some basics about HTTP.
Note that REST APIs don’t actually HAVE to use HTTP for communication, although HTTP is the most common choice. For the purpose of the CCNA you can assume that REST APIs use HTTP, but just keep in mind that HTTP is a specific communication protocol, and REST is a framework for building
APIs. They aren’t the same thing, and they aren’t always used together. Now, after the client’s HTTP request the server has to send a response.
Let’s look a bit at what is in that response. The server’s response will include a status code indicating if the request succeeded or failed, as well as other details.
You might not know it, but I’m sure you’ve seen at least one of these codes before. The first digit indicates the class, the type, of the response.
Here are the different classes. If the response code begins with a 1, it is an informational response. This indicates that the server received the request, but the exact meaning beyond that depends on the remaining two digits of the response code.
We’ll look at an example in the next slide. If the response code begins with a 2, the class is ‘successful’. This means the request was successfully received, understood, and accepted.
This is usually the class of response you want from the server. If the response code begins with a 3 the class is redirection. It means that further action is required to be taken in order to complete the request.
For example, if the URI of the resource has been changed, this response can tell the client to request the new URI to get the resource. If the response code begins with a 4, the response class is client error.
This means there was some error in the request and it can’t be fulfilled, such as requesting a resource that doesn’t exist. Let me show an example of that. The client sends a GET request for the resource at URI G, however that doesn’t exist on the server.
So, it sends a response with response code 404, indicating the resource was not found. I think many of you will recognize the 404 code, you’ve probably seen it on the Internet before.
For example, I tried to access google.com/jeremysitlab, and I got an error message. Notice the response code, 404, indicating that the resource was not found.
I got this error message because google.com/jeremysitlab doesn’t exist, at least not until Google acquires my company for the low price of a billion dollars.
If anyone from Google’s watching, I’m waiting for your offer. Anyway, there is one more class of response. If the response code begins with a 5 the class is server error.
This means that the request was valid, but the server was unable to fulfill it for some reason. For example, perhaps the server doesn’t support the HTTP version of the request.
Here are some examples of each HTTP Response class. I’ll just give one or two examples for each. For the informational class, you might see code 102, which means processing.
This indicates that the server has received the request and is processing it, but the response is not yet available. For the successful class you might see code 200, meaning OK.
This indicates that the request succeeded. If this is in response to a GET request, for example, the code 200 in the header will indicate it was successful, and in the message body the requested resource is sent.
Another example is 201, created, which indicates that the request succeeded and a new resource was created, for example in response to a POST request.
Remember, POST is equivalent to the CRUD Create operation. In the redirection class, 301, moved permanently, indicates that the requested resource has been moved, and the server indicates its new location to the client.
In the client error class we have 403, unauthorized. This means that the client must authenticate to get a response. Note that when talking about AAA in the security section of the course I told you about authentication and authorization.
They are different things. The official name for code 403 is unauthorized, but maybe a better name would be unauthenticated, because it indicates that the client needs to authenticate.
Another example is 404, indicating that the requested resource was not found. Finally, an example of the server error class is 500 internal server error, indicating that the server encountered something unexpected that it doesn’t know how to handle.
These are just a few examples, but there are many more HTTP response codes. If you’re curious about the others, check out this page, once again from Mozilla.
You can also check out this page for a summary of which Response codes you might see in response to each HTTP verb. Okay, that’s enough about HTTP.
Let’s finally take a look at some characteristics of REST APIs. REST stands for representational state transfer. And REST APIs are also known as REST-based APIs or RESTful APIs.
I usually just call them REST APIs. Note that, as I’ve said before, REST isn’t a specific API. Rather, it describes a set of rules about how the API should work.
It’s a framework for APIs. The six constraints, or rules, of RESTful architecture are a uniform interface, client-server architecture, stateless operations, cacheable (or non-cacheable) functions, a layered system, and optionally something called code-on-demand.
If you’re curious to know more details about all of these requirements check out this link, but now I will explain just a few of them that I think you should know for the CCNA.
The first is client-server. REST APIs use a client-server architecture, like HTTP. The client uses API calls, basically that means HTTP requests, to access resources on the server, such as in the example below.
The separation between the client and server means that they can both change and evolve independently of each other, they aren’t dependent on one another. And when the client application changes or the server application changes, the interface between them must not break, it should continue to function.
I think you’re fairly familiar with client-server architecture already, it’s used in various aspects of networking. The next characteristic I want to cover is that REST API exchanges are stateless.
I haven’t mentioned this term yet in the course, but it’s an important one. Statelessness means that each API exchange is a separate event, independent of all past exchanges between the client and server.
So, the server does not store information about previous requests to determine how it should respond to new requests. Each new request is a totally separate event.
And if authentication is required, this means that the client has to authenticate with the server for each request it makes. Although I haven’t used the term stateless, or stateful, in the course yet, I have introduced some stateful and stateless protocols.
For example, TCP is stateful. It establishes connections and uses sequence and acknowledgment numbers to keep track of the exchanges between hosts.
UDP, on the other hand, is stateless. No connection is set up and neither host keeps track of any sequence or acknowledgment numbers. The data is simply sent from host A to host B, UDP doesn’t keep track of the state of the exchange.
Now, I want to make a clarification. Although REST APIs use HTTP, which uses TCP as its Layer 4 protocol, HTTP and REST APIs themselves aren’t stateful.
Keep in mind that the functions of the layers of the OSI model are separate. Just because a stateful Layer 4 protocol is used doesn’t mean that the application layer protocol must also be stateful.
The last rule of REST APIs that I’ll mention is the cacheable OR non-cacheable requirement. Let me explain. REST APIs must support caching of data, basically that means storing data for future use.
You might have heard this term before in reference to web browsing. For example, your computer might cache many elements of a web page so that it doesn’t have to retrieve the entire page every time you visit it.
This improves performance for the client and reduces the load on the server. So, caching must be supported, but that doesn’t mean that every resource has to be cacheable.
Not all resources have to be cacheable, but cacheable resources must be declared as cacheable when the server sends them to the client, so the client will cache them for future use.
As I said before, if you’re curious about the other aspects of REST APIs you can check this link for restfulapi.net, but those three are the key ones you should understand for now.
And I’m repeating myself, but let me say this again. For applications to communicate over a network, networking protocols must be used to facilitate those communications.
And for REST APIs, HTTP is the most common choice, or more often HTTPS, its encrypted version is used. That’s all we’ll cover for REST.
For the CCNA, Make sure you know the CRUD actions, HTTP client request verbs, HTTP server response codes, and the basic characteristics of REST APIs that we covered in this video.
Now let’s move on to try some simple REST API calls using free resources provided by Cisco DevNet. So, what is Cisco DevNet?
I’ll let them describe it themselves. Cisco DevNet is Cisco’s developer program to help developers and IT professionals who want to write applications and develop integrations with Cisco products, platforms, and APIs.
There’s the magic word, API. Basically, DevNet offers lots of free resources such as courses, tutorials, labs, sandboxes, documentation, etc.
to learn about automation and develop your skills. It really is a great resource and it keeps getting better. Note that there is also a DevNet certification track that you can pursue if you’re interested in network automation.
If that’s the case for you, perhaps consider the DevNet associate cert after you get your CCNA. Today we will use their Cisco DNA Center Sandbox to send a REST API call using Postman.
DNA Center is one of Cisco’s SDN controllers, and we’ll cover it in the next video when we go more in depth regarding SDN. DevNet has various always-on sandboxes, which are devices you are free to access at any time and look around to get familiar with them.
In this case we’ll use their always-on DNA Center sandbox. And Postman, which we’ll use to send the API calls, is a platform for building and using APIs.
It’s also a great learning tool. So, to start I want you to make an account on DevNet, developer.cisco.com, and also make an account on postman.com and download the desktop app.
You can use it directly in your browser too, but I like to use the desktop app. For this demonstration we will simply follow this short tutorial on DevNet.
Go to this link or just do a google search for ‘devnet dna center getting started’, it should be the top result. Note that you don’t have to follow along, you can just watch me do it, but I do recommend that you try it out yourself.
But even if you’re just following what I’m doing, make sure you read the tutorial on DevNet too because it will explain the reason for each of the steps. I’ll just show you them.
So, I’ve installed the Postman desktop app and opened it. From the home screen, click on workspaces and and select My Workspace.
If you don’t have a workspace you can create one by clicking where it says ‘create workspace’. Now I’m in my workspace.
Next you should click New here. That will open this window, where you can select what you want to create. For example, you can create a new API, documentation for an API, a mock server to test APIs, etc.
In this case, we want to create an HTTP request which we will send to DevNet’s DNA center server. After clicking on HTTP request, we can now specify what kind of HTTP request we want to send to DNA center’s REST API.
As explained in the DevNet tutorial, first we have to generate an authorization token which we will use for our second HTTP request. First, set the HTTP verb to POST, it is GET by default.
Then we have to specify the URL. Just a quick note, the DevNet tutorial uses the term URL, Uniform Resource Locator, but I used the term URI, Uniform Resource Identifier, earlier in the video.
Basically, a URL is just a type of URI. You can google ‘URI vs URL’ for more info. So, if we send a POST request to this URL, an authorization token will be generated.
However we have to specify a username and password for that to happen. To do that, click on authorization here, and then select Basic Auth.
That will open this menu, where you should enter these credentials. Username devnetuser, password Cisco123! with a capital C and an exclamation point at the end.
Okay, the POST is ready so now we can click send to send the POST request to DNA center’s API. If the POST is successful you will get a response with code 200, OK.
The body of the response message will include your authorization token in JSON key-value format. Make sure to copy the value of the token. You don’t have to copy the quotation marks around the string, just the part I’ve highlighted.
Okay, now we’re ready to send the second API call, which will retrieve a device inventory from the DNA center controller. First, change the HTTP verb to GET.
We will also send this request to a different URL, as written in the tutorial. And the next step is to add an additional HTTP header with our authentication token.
To do that, click on ‘headers’ here, enter ‘X-Auth-Token’ for the key, and in the value field paste the authorization key you got from DNA center with the previous API call.
Okay, now we’re ready for the second API call, click on send to send the GET request. If the GET is successful, you will once again get a response with a 200 OK code.
And in the body we get the requested information, JSON-formatted data listing the inventory of devices managed by the DNA center controller. The output here is quite long, but let me show output for one of the devices.
Here is that output. You should be familiar with the format, as I mentioned it’s JSON. So, by looking at this output we can learn about this device that is managed by the DNA center controller.
For example, the device family is listed as switches or hubs. Since hubs aren’t really used anymore, it’s safe to say that this is a switch.
The role is access, so it’s an access layer switch. The hostname is leaf1.abc.com. Leaf1, perhaps that means this is a leaf switch in a spine-leaf architecture.
Here’s the platform ID, C9300-24U. This is the device model, Catalyst 9300. Here’s a picture of a C9300, by the way.
We can also get info like the uptime, how much time has passed since the device booted up. So, that was a quick look at some basic REST API calls to Cisco DNA Center using Postman.
We retrieved data in a machine-readable format, JSON, so we can write applications that gather data like this, interact with it, tell DNA center to change it, etc.
We’ve barely scratched the surface, but I hope you’re starting to see the utility of APIs. Here’s what we covered in this video.
We briefly reviewed what an API is, and then looked at CRUD operations and their equivalent HTTP verbs. Then we looked at the basic characteristics of REST APIs, and finally tried a few REST API Calls to DNA Center using a Cisco DevNet tutorial.
As I mentioned earlier, make sure you know the CRUD actions, HTTP client request verbs, HTTP server response codes, and the basic characteristics of REST APIs that I covered in this video.
I also gave various links to different resources throughout the video that I recommend reading, although they will go in much more depth than is required. But check them out if you want a greater understanding.
And make sure to watch until the end of the quiz for a bonus practice question from Boson Software’s ExSim, the best practice exams for the CCNA. Okay, let’s go to quiz question 1.
What HTTP response code would you expect to receive if you tried to GET a resource that doesn’t exist on the server? Pause the video now to select the best answer.
Okay, the answer is B, 404. This code means Not Found. If the requested resource does not exist on the server, you can expect this response code.
Okay, let’s go to question 2. Which of the following is NOT a constraint of RESTful architecture? Pause the video now to select the best answer.
Okay, the answer is C, stateful. REST APIs are stateless, meaning that each individual client request is a unique event, not dependent on any previous requests.
In addition to these five constraints, there is an optional one called ‘code on demand’, which basically extends client functionality by allowing them to download and execute code from servers.
Okay, let’s go to question 3. Which category of HTTP response would you expect in response to a successful request?
Pause the video now to select the best answer. Okay, the answer is B, you should expect a response code beginning with 2.
This indicates a successful request, for example with the 200 OK response code or the 201 Created response code. Okay, let’s go to question 4.
HTTP verbs PUT and PATCH are equivalent to what CRUD operation? Pause the video now to select the best answer.
Okay, the answer is C, U, which stands for update. Note that you might also see PUT categorized as a Create action, because it can be used to create new variables, but usually it’s used as an Update function.
Okay, let’s go to question 5. Which of the following would you expect to find as the scheme of a URI?
Pause the video now to select the best answer. Okay, the answer is A, HTTPS.
The scheme of a URI identifies the protocol you are using, typically HTTP or HTTPS for web services and REST APIs. Okay, that’s all for the quiz.
Now let’s take a look at a bonus question in Boson Software’s ExSim for CCNA.