All right.
Welcome back.
So we're going to move on from high level talking about what AP guys are how they work and how they
are structured and what they respond with.
Jay Sonic's M-L we're going to move on from that to actually getting dirty with AAPI is actually working
with them incorporating them into an application.
Before we do that.
Going to do a quick refresher on how you can make h h t t p requests.
So the most common way that you've seen probably your whole life on the web is just using the browser
.
So I can open up a window and go to Google dot com.
I made a request.
I hit enter I click this button that makes me a request.
I do a search for dog.
That's a request I click on that dog link to wikipedia and that's a request.
OK.
So that's one way.
But I can also make requests through code which is what we're going to learn about here is how do I
write javascript that will make a request for me.
How do I write javascript that will fetch Jaison from the Yahoo weather API or from the Reddit API.
That's our whole goal here is to write some code that will go and get information from an API through
a request and then do something with it like save it to a database or show it to a user.
And the secret to that is that you don't have to make a request through the browser.
You can make a request through code.
We're going to see how to do it using a package called request oddly enough but there's also other ways
of doing it.
A tool called Currall see you Arel is a way to make requests from the command line.
So not necessarily for an application but just to show you that requests are you know independent browsers
I can request HGP colon slash slash www.youtube.com from my terminal and I get this content the exact
same response that I do when I go to Google dot com.
The key difference of course is that in a browser that information is then rendered back to me.
So I see this but the underlying text him that sent back is what I'm seeing here.
So most the time in our code we're not going to be asking for him out from a web page.
I mean this is such a mess to work with.
Instead we're usually asking for Jaison or X-amount from an API but the same idea holds true.
I can request an API using Currall.
So I just do Kerl on an API you IRL but that's not what we're going to do really for most applications
.
That's just kind of an educational demonstration that you can do it from the command line.
We're going to do it inside of an application and we're going to start really really simple.
We're going to show you how you make a request in node.
So I'm going to make a new directory here API eyes and then I'm going to make a new file.
I'm just going to call it first request.
Jay Yes.
So all we're going to do is make a request from a javascript file.
So we'll just request Googlebot column again which is what we do.
Which is what we just did using cURL.
And I know it's kind of all this can be a lot to take in.
The whole point is just we made a request in the browser by refreshing the page.
We made a request and the command line.
It works the same way as far as the content we get back.
Now let's do the same thing.
But from a javascript file.
So I'm going to open up that file.
And we want to put our code here that will make the request.
So how do we do that.
How do we make a request from node.
There's a lot of different libraries packages you can use.
The one we're going to use is called request and this is probably the most common so get have that com
slash request to get you there.
Just google node request simplified aged UDP request client.
So this is how we can make a simple request.
This is kind of the basic syntax.
If you take a look we first included by requiring request setting into a variable just like every other
package then what we do is make a request to a u r l and then we have a function that we pass it and
there's multiple parts of this function or multiple arguments and error response and body.
So we use a callback here because making a request takes time sometimes will be very fast a matter milliseconds
.
Sometimes the request if you're requesting a web page that doesn't exist or a server that's down it
will get hung up.
And so you need to wait often times to do anything until you have a response whether it's good or bad
.
So we'll see and we'll talk about what these are and how you use them.
For now let's just copy this over and I'm actually going to retype it line by line but I'm just going
to keep it here so you can see it.
And the very first thing we need to do of course is install the package request so NPM install request
that you go get a request and we'll be able to require it correctly appear.
OK.
So we've done that.
I'm going to just resize a little bit here and there is going to rewrite this.
Rewrite this or we can go over it.
Require requests.
Nice and simple to start.
So the next line is actually making that request.
So this app if you want to call the nap whenever we run it we'll just run node first request.
Yes.
All it will do is print out the iChat him out from Google.
They'll be the first thing we do.
So have our request equals require request.
Then we make our request so we'll do HTP colon slash slash W.W..
Google.com.
Exactly what we did with Currall the same you are ill and we expect the same age timeo.
So that's the first part that will make the request.
But to do anything with the information that comes back we need a callback.
And as they explained that's because it's not instantaneous.
It takes time.
This request has to go all the way to a server somewhere potentially thousands of miles away.
Then come all the way back to us and then our computer has to understand it and make sense of it.
So there are three things that we pass in error response and body.
The first one which is error is will hold any potential error that we get.
So if there's a problem the server does is down or the request times out or we don't have an internet
connection for instance if I turn my wife off and try that we'll get an error back.
So we'll see how to play with that in a little bit.
But it's important that you check for an error.
So we'll do something like this if there's an error and then we'll have an else.
So if there's an error let's just cancel don't log.
Something went wrong and then will also cancel that log the error itself.
Otherwise that means that we got something back.
You can see there's a second thing that they're checking for here in this example if response does status
code is 200.
So we're not going to go into status codes all that much but you probably are familiar with the 404
status code whenever you try and request a page that doesn't exist anymore.
If we look up for a for status code that not found error message that you see you know if you're trying
to request something that can't be found.
There are a lot of other status codes though.
So you can see here the most important one though is 200.
Stands for.
OK it's a standard response for successful requests.
So if things went well and we got a response we want to check if it's status code is 200 because we
could potentially get a response that has a status code of something else other than 200 like the page
has moved.
And that's not going to be two hundred and we want to handle that differently.
So this is something you can kind of just hard not hard code but you can always assume that you want
to check if the status quo is 200.
So we'll do that in here for now is a nested IF statement so if response that status code equals equals
200.
So if we get here things worked OK.
So if that happened let's just do a to that log.
And we're going to printout body.
So that's the last piece of information which is the actual response body the stuff that came back that
will be our or exceed our age to him out from Google.
So there's a lot to take in.
I'm going to get rid of this for now.
Just cut that all out.
Save my file and let's see what happens.
So node first request Yes that's the name of our file.
And wow we get all the age Tim print printed out.
Great.
So let's also try changing that you are.
Let's go to read it dotcom safe file.
Run it.
And here is I read it to him all we could do the exact same thing with an API.
So if we go back to the Yahoo weather endpoint this is to get the sunset time for Hawai not expecting
you to type this or follow this exactly.
But if I pace that you are here this giant you are l This is the API for Yahoo weather that will give
me sunset time.
Let's see if it worked.
And did we have that information back.
Awesome.
So I'm actually going to get rid of that for a second.
Now let's take a look at if I change some things.
So what if my request HTP colon slash size.
I mean I don't Google and a bunch of random stuff dotcom and I save the file and I make a request I
get something went wrong.
So that means we've got an error.
And on top of that the error itself has some information from Consta that log error the error is not
found.
So our page wasn't found.
So that's the use of the checking for the error is you can make sure that the request worked.
So if we didn't we just automatically handled it.
And in this case already is printing the body but let's say we're saving data to the database.
We want to make sure that we got the data first and at the request worked and we didn't make it to the
wrong place or that you know something went wrong.
But we can simplify this all as you can see up here.
This is the shortened version where you just have one if statement checks for both of those at the same
time.
All right so that's how we make a request from node using this request library or request package.
Very simple in the sense that it's pretty much always going to be the same.
You're just going to require requests and then you're going to add that you are Elen that your request
and will change us back to just Google dot com.
Then you have your callback function.
Three pieces of information check for an error check for the status code and the response and then do
something with the body.
Usually you don't just print it out but this is where we would do something with it is if there's no
error and status code is 200.
So I know it's a lot but it's kind of a recipe you can follow.
OK.
So in the next video we're going to explore a little bit more with API is actually request to Jason
API and do something with it.
Great.