What Is A RESTful API? Explanation of REST & HTTP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you guys welcome to another web technology informational video in this video we're going to talk about what a REST API is now I do have several videos on my channel showing you how to create a REST API of couple in JavaScript and node and one in PHP so you may have seen some of those but you may still have some confusion on how a REST API really works now you may be saying well I can just Google this what I have to watch the visit this video well if you do google it you'll get a thousand different explanations that are so complicated that it's nearly impossible for an entry level developer to really grasp it so I try to explain things on a more basic level and hopefully that's why you guys watch my videos I try to take complicated concepts and explain them in plain English I'm not a real sophisticated guy so I like things spelled out without nonsense so that's what I try to give my viewers alright so before we get into rest let's talk about what an API ends in general so it stands for application program interface and that's a very broad term there's all kinds of api's but we're specifically talking about web api's there's API senior computer operating system in your smartphone and even in some refrigerators and so on so this is very generalized but it's essentially a contract provided by one piece of software to another piece of software it usually consists of a structured request and then a structured response so one piece of software it says give me this information formatted in this way and I'll give you this data or this function or whatever that response may be alright so to help you understand we're going to take a look at a couple analogies this first one is actually from another YouTube video which I'll link in the description I'm using it because it's one of the best analogies I've seen so think of yourself at sitting at a table at a restaurant in your one piece of software such as a web application on the client side and then the kitchen is the server or the service that processes certain requests well the waiter is like an API is formatted to take a certain order in a specific way and then bring back a certain meal this would be the data or the response that you requested so an API is basically a messenger or a waiter between running software all right so another good if another good analogy is this box here this toy so if we think of this as an API it's formatted to take certain shapes so the client has to format the request as a circle square or triangle anything else will be accepted you'll get some kind of error message or error response so this is similar to how an API works and you can think of these shapes as the API standard whether it's Jason or soap or something else so to make this operate even more like a real API we could have some kind of output once the formatted shape requests go in and that would be your response okay so hopefully this gives you a good idea as to what an API is in general all right so now that we talked about what an API is let's talk about rest so it stands for representational state transfer and it's an architecture style for designing Network applications it works on relying on a stateless client-server communication protocol and in almost all cases this is going to be HTTP all right you guys know what HTTP is it's basically the foundation of the communication of the Internet of the web every time you load a web page in your browser it's making an HTTP request to a server somewhere it is possible to use other protocols with rest but HTTP is by far the most used because in order to use real-world rest you need the delivery methods that HTTP offers rest with me to treat objects on the server side as resources that can be created updated and destroyed or deleted an example of a server-side object would blog post in a database or something like that okay we can have we can create these posts with a post request a delete with a delete request and so on alright and what makes resto awesome is that it operates using just HTTP and usually some kind of standard like Jason so it can be used by virtually any programming language because most of the good languages can make HTTP requests in some way whether it's PHP JavaScript rails Java Python all of these languages are perfectly capable of working with Rexel interfaces alright so hopefully I haven't lost yet just remember an API is is the messenger and rest lets us use HTTP request to format those messages you'll we may also hear the term restful api and that just refers to conforming to the rest constraints so rest api and restful api are essentially the same thing so now that we've established what a rest api is let's look at the specific methods and request that can be made to a server through HTTP okay so a get request is the most common your browser client makes get requests every day just by going to a specific server uri get requests are used to do just that they get data or retrieve data from a specified resource okay next we have a post request and you probably use these everyday as well because every time you fill out a webform you're making a post request in most cases you can also make get requests from forms but it's not secure and the data you submit can be seen by anyone form form tags in HTML can take an action and a method attribute so the action would be the page that you're submitting to and then the method would be either post or get okay those are the only two requests that can be made from just a web form on its own so next we have put and put will update any specified resource usually you would have send requests to an endpoint which is a URI with some kind of ID and for that specific resource whether it's a blog post or a product the resource itself doesn't matter but the server needs to know which which one you want to update okay can't make a put-up put request from a forum like you can a post you'd have to use something like Ajax you could use a standard JavaScript or you could use jQuery or something like that and then if you're using a framework like angular you have different modules including an HTTP module that's capable of sending puts and deletes okay a delete request is just that it will delete a specified resource on a server and again you have to let the server know what you're deleting so you want to send an ID along with that now there are other types of requests but they're very rarely used ahead request is the same as yet except it doesn't return a body in the response it will only return the head info options can be used to see the supported methods of the server and then patch is for partial resources updating partial resources but we're not going to get into that okay so let's look at some endpoint examples endpoints are the URI or the URL that our HTTP requests are sent to so here's an example using a sample API at my site comm slash API okay you can choose whatever folder you want to put your API endpoints in but many many times you'll use an API folder or even an API subdomain okay so this first one here is a as a endpoint for a get request and this would typically just give you a list of users ok would return a response with all the users and we can make a get request to a specific user by going API slash users slash and then whatever that users ID in many cases you may have an endpoint like this where you're saying instead of just users and then the ID it would be users details and then the ID or users show or something like that now in this case we're making a post request to API users and that's going to add a user to to the server to the database now notice that this get request and this post request have the same endpoint but since they're different methods are different requests that's okay okay you can use the same the same URL but they have to be different requests in this case here where he's making a put request to API users one or you may see something like this API users update one and that's going to update that user okay you'll have to send data along with that just as you would with a post request and then delete you can make a delete request to API users one and that will delete that user okay or you may see something like this users delete one okay now if we use endpoints like the ones we just saw without any kind of authentication of who we are those are public or open API s but sometimes you need to authenticate before using them now this can mean just register registering your app with the providers website and even sometimes you'll have to pay for it you'll have to purchase that data access so there's a few ways that authentication is implemented so usually you'll use OAuth which involves getting some kind of access token and sending that along with your requests so if you tend to attempt to make a request without that then you'll get some kind of unauthorized ever so here's some examples of how it works with the github API which is a really nice API for beginners it's really easy to use and how it works is you can use it without any kind of authentication but only up to I think it's like a hundred requests per hour if you go over that and you didn't authorize then you're going to get an error and you're not going to be able to fetch the data so there's a few different ways that this is implemented and these examples were using curl to make our requests curl is just basically a tool that we can use to transfer data using multiple protocols including HTTP so this first one here is by sending the token inside of the header okay inside the HTTP header so you can include a value for authorization and you would set that to your token all right sometimes you can even send the token as a parameter in the URI and another common way is to not send the token itself but a generated client ID and secret as a parameter to your request alright so what I want to do now is jump in and take a look at a real world example we're going to look at the github API we're going to look at some of the documentation and I'll show you what happens when you make a get request and we'll request some user data all right so this is the documentation for github API version 3 if you go to developer github comm v3 and this just basically gives you a bunch of information on the API and most public API or most data guys in general have some kind of documentation like this so you know what endpoints you need to use what methods are ok to use things like that so if we take a look down here see if we look at HTTP verb so so these are the different types of requests that can be made in certain endpoints so head get post patch put and delete it shows us how you can authenticate and this is the information we saw on the slide it shows us that we can authenticate through the header through a parameter and so on if we scroll down here some more it'll also show you how to use pagination and you can use that by requesting a certain amount of of whatever it is users or repository so in this case of saying 100 per page and then you can also specify the page and then you can use your programming logic to create pagination okay so you only so you have a hundred per page and so on let's see if we go down some more see actually that's not what I want to show you let's go back up here and then over here I want to click on users okay so this gives us the how to get a single user we make a get request to slash users and then whatever that user name okay when you see a colon in front of it like that that usually means that this is some kind of placeholder so this would be whatever username you want and then this shows you the actual response you get okay so 200 status means everything's okay and then it gives you this in the body okay gives you Jason of all that users information including their user name their ID the URL to their image their to their avatar see how many see how many repos they have how many gifts their blog their company and then all these URLs as well which you can make other requests to and if we go down some more the update the authenticated user so if you are authenticated as a specific user you can actually make request to update your information okay so you can edit your profile if you want to get all users you can do that by just making a get request to slash users so what I want to do is try this out now you can obviously do this within your your program but there's also tools that you can use to make request ok postman is what I suggest you use you crumb a google chrome plugin or extension whatever it's called so you can go ahead and download that and then all you have to do is put your endpoint in here and then the type of request in some cases you'll have to send data or maybe some header values for instance if you need to do authorization you can put that in here and you can put the value so these are key value pairs if you need to send data you can do that as well usually you can only send data you can't do it with a get request you can see that's grayed out but if it was a post request you can send the body in different or Matt's you can send it as a form or you can send raw data or for instance raw Jason alright but what I'm going to do is make a get request to HTTP API dot github.com slash users okay and then this is what it gives us and you can see we have basically it's an array of user objects so this is the username the ID all the URLs and all that stuff okay now if we want to get a specific user we could say we can make a get request to just put my username send and then that gives us all that information on that specific user okay now with the github API and a lot of API you can do it like this without any kind of authentication for a certain amount of requests in fact let me see if I can get to it it may here we go and you can see API rate limit has exceeded so you get a certain amount of requests per hour or per cup every couple hours or whatever it may be whatever they set it to so what we would have to do now to be able to keep using this in our application is to register it okay so if we go to github.com settings applications new and let's say test app home page just a test comm application description just in my app and then some kind of callback URL we'll just take this and let's say register application and now you can see it gives us a client ID and a client secret so what we have to do now is just include these as a parameter okay so if we go back to our documentation here and go down to our where is it authentication so we can add we can send these as a parameter you can see right here client ID client secret so let's take that starting with that question mark go back to postman and we'll just grab the client ID and we'll put that right here place these X's and then a secret and put that right here we don't want that quote on the end and now let's make let's do send and now we can get it okay we can keep making them because we're authenticated all right so that's going to be it for this video hopefully this gives you guys a better idea on how RESTful API is work and if you haven't seen my videos where we create a restful api i would suggest doing that alright so that's it thanks for watching and I will see you next time
Info
Channel: Traversy Media
Views: 1,249,284
Rating: 4.9184909 out of 5
Keywords: rest api, restful api, what is rest, what is an api, api, what is rest api, http requests
Id: Q-BpqyOT3a8
Channel Id: undefined
Length: 18min 37sec (1117 seconds)
Published: Mon Feb 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.