What Is A RESTful API? Explanation of REST & HTTP
Summary
TLDRThis video tutorial breaks down the concept of REST APIs, explaining how they function and their importance in web technology. It simplifies the idea of APIs as intermediaries between software, using analogies like a restaurant order system and a shape-sorting toy. The presenter covers REST principles, HTTP methods, and how to interact with APIs using tools like Postman. The video also touches on authentication methods, demonstrating how to use the GitHub API with and without authentication, providing practical insights for developers.
Takeaways
- đ API stands for Application Programming Interface and is a broad term for various types of interfaces, including web APIs.
- đœïž An analogy for understanding API is likening it to a waiter in a restaurant who takes orders (requests) and delivers meals (responses).
- 𧩠REST stands for Representational State Transfer, an architectural style for designing networked applications that rely on stateless client-server communication.
- đ REST APIs use HTTP methods such as GET, POST, PUT, DELETE to interact with resources on a server.
- đ The video creator emphasizes explaining complex concepts like REST APIs in plain English to make them accessible to entry-level developers.
- đ REST APIs treat server-side objects as resources that can be created, updated, and deleted using HTTP methods.
- đ REST APIs operate using HTTP and often a standard like JSON, making them usable by virtually any programming language.
- đ Endpoints are URIs or URLs where HTTP requests are sent to access or manipulate resources.
- đ Authentication may be required for APIs, often implemented through OAuth, which involves obtaining an access token to include with requests.
- đ ïž Tools like Postman can be used to test API requests without writing code, allowing for the easy sending of various types of HTTP requests.
- đ The GitHub API is highlighted as a good starting point for beginners to understand and use REST APIs.
Q & A
What is the main purpose of a REST API?
-A REST API is designed for networked applications and allows for the creation, updating, and deletion of server-side objects, such as blog posts in a database, using HTTP requests.
What does REST stand for and what is its role?
-REST stands for Representational State Transfer. It is an architectural style for designing networked applications that rely on stateless client-server communication protocols, typically HTTP.
How does the restaurant analogy help explain what an API is?
-In the restaurant analogy, the waiter acts as an API, taking a structured order (request) and bringing back a specific meal (response), illustrating how an API facilitates communication between software components.
What is an endpoint in the context of REST APIs?
-An endpoint is a URI (or URL) to which HTTP requests are sent. It represents a specific resource or action that can be performed on a resource in a REST API.
What are the common HTTP methods used in REST APIs?
-The common HTTP methods used in REST APIs are GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH, with GET and POST being the most frequently used.
Why is JSON often used with REST APIs?
-JSON (JavaScript Object Notation) is often used with REST APIs because it's lightweight, human-readable, and easily parsed by most programming languages, making it ideal for data interchange.
What is the difference between a REST API and a RESTful API?
-A REST API is a set of guidelines for designing networked applications, while a RESTful API is an API that conforms to these REST constraints, essentially meaning they are the same thing.
How can authentication be implemented in a REST API?
-Authentication in a REST API can be implemented using various methods such as OAuth, which involves obtaining an access token, or by sending client ID and secret as parameters with the requests.
What is the significance of the 200 status code in an HTTP response?
-A 200 status code in an HTTP response indicates that the request has succeeded, and the response returned contains the requested data or information.
How can you test a REST API without writing code?
-You can test a REST API without writing code using tools like Postman, which allows you to make various types of HTTP requests to endpoints and view the responses.
What is the GitHub API and how is it used in the script?
-The GitHub API is a RESTful API that allows access to GitHub data and functionalities. In the script, it's used as a real-world example to demonstrate how to make authenticated requests and handle API rate limits.
Outlines
đ Introduction to REST APIs
The video begins with an introduction to REST APIs, emphasizing the aim to simplify complex concepts for beginners. The presenter clarifies the term API, standing for Application Program Interface, and explains it as a broad term applicable to various software interactions. A REST API, in particular, is focused on web APIs and operates based on HTTP requests. The video promises to demystify REST APIs with basic analogies, such as a restaurant scenario where the waiter represents the API, facilitating communication between the client (diner) and the server (kitchen). Another analogy involves a toy that only accepts certain shapes, illustrating how APIs require specific formats for requests.
đ Understanding RESTful Methods and Requests
This section delves into the specific methods and requests used in RESTful APIs, starting with the GET request, which is fundamental for retrieving data from a server. The presenter explains that POST requests are commonly used when submitting web forms, while PUT requests are for updating resources, typically requiring an ID to specify which resource to update. DELETE requests are used to remove resources, also needing an ID for specificity. The video also touches on less common requests like HEAD, OPTIONS, and PATCH. Examples of endpoints, or URLs where HTTP requests are sent, are provided, showing how different methods can be used on the same endpoint.
đĄïž Authentication in RESTful APIs
The script discusses the importance of authentication in RESTful APIs, explaining that while some APIs are public, others require authentication to access. It outlines various authentication methods, including OAuth, which involves obtaining an access token to include in requests. The GitHub API is used as an example to demonstrate how authentication can be implemented, either by including a token in the HTTP header or as a parameter in the URI. The presenter also mentions the need to register an application to obtain a client ID and secret, which can be used for authentication purposes.
đ Practical Example Using GitHub API
The final paragraph provides a practical example using the GitHub API, guiding viewers on how to make authenticated requests using tools like Postman. The presenter demonstrates how to make a GET request to retrieve user data and explains how to handle API rate limits. The process of registering an application with GitHub to obtain a client ID and secret is outlined, which is necessary for continued API access beyond the initial limit. The video concludes with a recap of the information covered and an encouragement for viewers to explore the presenter's other videos on creating RESTful APIs.
Mindmap
Keywords
đĄREST API
đĄAPI
đĄHTTP
đĄStateless
đĄJSON
đĄGET request
đĄPOST request
đĄPUT request
đĄDELETE request
đĄAuthentication
đĄEndpoints
Highlights
Introduction to REST API and its basics
Explanation of what an API is in general
The role of an API as a 'messenger' or 'waiter' between software
Analogies used to explain how APIs work
REST stands for Representational State Transfer
REST is an architectural style for designing networked applications
HTTP is the foundation of REST communication
REST treats server-side objects as resources
Advantages of REST due to its use of HTTP and standards like JSON
The difference between REST API and RESTful API
Explanation of HTTP methods: GET, POST, PUT, DELETE
Endpoints are the URI or URL for HTTP requests
Examples of endpoint usage in REST APIs
Authentication in REST APIs
How to authenticate using tokens, client ID, and secret
Real-world example using GitHub API
Using tools like Postman to make API requests
How to handle API rate limits and authentication
Summary and conclusion of the video
Transcripts
hey 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 uh showing you how to
create a rest API a couple in in
JavaScript and node and one in PHP um so
you may have seen some of those but you
you may still have some confusion on how
a rest API really works now you may be
saying well I can just Google this why
do I have to watch the V 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 uh I'm not a real sophisticated
guy so I like things spelled out without
nonsense um so that's what I try to give
my view ERS all right so before we get
into rest let's talk about what an API
is in general so it stands for
application program interface and that's
a very broad term there's all kinds of
apis uh but we're specifically talking
about web apis um there's apis in your
computer operating system in your
smartphone and even in some
refrigerators and so on so um this is
very generalized but it's essentially a
contract provided by one piece of
software to another piece of software uh
it usually consists of a structured
request and then a structured response
so one piece of software 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 all right 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 anal IES
I've seen so think of yourself at
sitting at a table at a restaurant um
and you're 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 he's
formatted to take a certain order in a
specific way and then bring back a
certain meal uh this would be the data
or the response that you requested so an
API is is basically a messenger or a
waiter between running
software all right so uh another good
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
uh anything else won't 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 the
shape
as the API standard whether it's Json or
soap or something else um 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 a 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 networked
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 the
foundation of the communication of the
internet of the web uh every time you
load a web page in your browser it's
making an HTTP request to a server
somewhere um it is possible to use other
prot calls with rest but HTTP is by far
the most used um because in order to use
real world rest you need the delivery
methods that HTTP offers um rest was
made to treat objects on the server side
as resources that can be created updated
and destroyed or deleted um an example
of a serers side object would be a blog
post in a database or something like
that okay um we can have we can create
these posts with uh a post request um a
delete with a delete request and so on
all right and what makes rest so awesome
is that uh it operates using just HTTP
uh and usually some kind of standard
like Json so it can be used by virtually
any programming language uh because most
of the 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 restful
interfaces all right so hopefully I
haven't lost you yet just remember an
API is is the messenger and rest lets us
use HTTP requests to format those
messages um you you may also hear the
term restful API and that just refers to
um conforming to the rest constraints so
rest API and restful API are essentially
the same
thing so now that we've established what
AR rest API is let's look at the
specific methods and requests that can
be made to a server through
HTTP okay so a get request is the most
common your your browser client makes
get requests every day just by going to
a specific server URI uh get requests
are used to do just that they get data
um or retrieve data from a specified
resource okay next we have a post
request and you probably use these every
day as well because every time you fill
out a web form 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 a specified resource usually you
would have to send a request to an
endpoint which is a URI with some kind
of ID in for that specific resource
whether it's a blog post or a product um
the resource itself doesn't matter but
the server needs to know which which one
you want to update okay you can't make a
a put request from a form 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 um 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 a
head request is the same as get except
it doesn't return a body in the response
it'll only return the head info options
can be used to see the supported methods
of a 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 HTT P requests are sent to
so here's an example using a sample API
at mysite.com
API okay you can choose whatever folder
you want to to put your uh API endpoints
in but many many times you'll use uh an
API folder or even an API subdomain okay
so this first one here is a is a um
endpoint for a get request and this
would typically just give you a list of
users okay it would return a response
with all the users and we can make a get
request to a specific user by going API
users slash and then whatever that
user's 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 um to the server or to the
database now notice that this get
request and this post request have the
same endpoint but since they're
different methods or 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
we're 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 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'll delete that
user okay or you may see something like
this uh 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 apis
but sometimes you need to authenticate
before using them now this can mean just
register registering your app with the
provider's 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 ooth
which involves getting some kind of
access token and sending that along with
your requests so if you temp attempt to
make a request without that then you'll
get some kind of unauthorized error 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 um and how it works is you can
use it without any kind of
authentication but only up to I think
it's like 100 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 in these
examples we're using curl to make our
requests curl is just basically a tool
that we can used 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 uh to your
request all right 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 going to look at some of the
documentation and I'll show you uh what
happens when you make a get request and
we'll request some user
data all right so this is the
documentation for github's API version
three uh if you go to developer
github.com
V3 and this just basically gives you a
bunch of information on the API and most
public apis or most apis in general have
some kind of documentation like this so
you know what endpoints you need to use
what uh methods are okay to use things
like that um so if we take a look down
here uh let's see if we look
at uh HTTP verb so so these are the
different types of request that can be
made in certain endpoints so head get
post patch put and delete um it shows us
how you can authenticate and this is the
information we saw on the slide shows us
that we can authenticate through the
header through a parameter and so on uh
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 um of whatever it is users or
repositories so in this case it's 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
100 per page and so on um let's see if
we go down some
more let's 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 sing single user we make a get
request to/ users and then whatever that
username okay when you see a colon in
front of it like that that usually means
that this is some kind of placeholder um
so this would be whatever username you
want and then this shows you the actual
response you get okay so a 200 status
means everything's okay um and then it
gives you this in the body okay it gives
you Jason of All That user's information
including their username their ID the uh
URL to their image to their to their
Avatar um let's see how many um let's
see how many repos they have how many
gists their blog their company and then
all these URLs as well which you can
make other request to and if we go down
some
more let see update the authenticated
user so if you're authenticated as as a
specific user you can actually make
request to update your information okay
so you can edit your profile uh 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 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 requests okay
Postman is what I suggest use you can
it's a chrome uh 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 uh
in some cases you'll have to send data
or maybe some header values uh 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
um and if you need to send data you can
do that as well usually if 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 different formats uh you
can send it as a form or you can send
raw data for instance raw Json all right
but what I'm going to do is make a get
request to
https
api. github.com
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 um all
the URLs and all that stuff okay now now
if we want to get a specific user we
could say we can make a get request to
I'll 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 apis 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 yeah here we go and you can see
API rate limit has exceeded so you get a
certain amount of requests per hour or
or per every couple hours or whatever it
may be whatever they set it to um 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 homepage
just say
test.com application
description just say 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 uh 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 and go back to
postman and we'll just grab the client
ID and we'll put that right here place
these these x's and then the
secret and we'll 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 uh
restful apis work and if you haven't
seen my videos where we create a restful
API I would suggest doing that all right
so that's it thanks for watching and I
will see you next time
Voir Plus de Vidéos Connexes
5.0 / 5 (0 votes)