All right.
Welcome back.
So we saw how to use the request package to make requests instead of a node app or a node file in this
case.
We're now going to talk about how you make a request that handles Jaison.
So we got all of this crazy aged him from read it and from Google.
Most the time you won't be doing that.
Most of the time you'll be requesting an API.
So we're going to take a really simple one.
This Yahoo weather API that we saw that's just set up to get sunset time in Hawaii.
So I'm going to copy that you url.
Just double check.
This is what we get back.
And all we want to do is make a really simple application that every time you run it it just prints
out the time set for today or the or the next sunset in Hawaii.
So not that exciting but nice and simple Jason that we'll handle and what we'll want to do is print
out.
It looks like let's see results channel Astronomie sunset.
So need to access those four levels down to get this 6:55 p.m. string.
And this is just like a regular javascript object.
So let's go back and just Adeptus first thing that we'll do is just let's change the surroundings to
match this form that we have here.
So if no error and response status code 200 just to clean it up then I'm going to get rid of this else
.
And then in here.
Well we want to do just get rid of all that
and just do cancel the log response or conses that log body C-3 and will change the URL.
So just start simple.
Copy that.
Copy that you Ereli again paste it in.
It's gigantic.
I know.
Just while we're looking at this you can see there's all these percent.
Those are how you encode a space and you are all just in case you weren't familiar with that.
There's this giant statement here.
It's pretty intense.
We're not going to focus on that if you wanted to use this API for something on your own.
You could definitely check it out but you can see here.
There's Maui why I'm sure we could replace this with some other location like in Alaska and we would
expect sunset potentially to be different up north.
I don't know.
OK so let's just see what we get when we print out the body and let's run this picture.
Other stuff is coming out.
Node first request.
Yes.
OK.
So we get this printed out.
So all we really need to do is access queery and then we need to go access results.
It's kind of hard to see here.
Channel astronomy sunset.
But again if you look at it here the way it's structured and this is just a nice kind of tool they use
to help people understand what the response looks like.
Most AP guys won't have the exact format where you can view it but of course you can just make the request
here in your browser and take a look especially if you have just one view.
So I'd like to do this query.
OK.
So that's the first level and then the next thing is results.
So let's just start with queery.
So if we go back we should just be able to do cancel that log body query just like that save it.
Let's try it.
Oh no undefined.
Why would that be happening.
So what's going on.
Well the answer is that when we just get the body back here it looks like it's a javascript object but
it's not.
It's a string.
So take a moment and think about how we could prove that this is a string.
Constable Lague body how do we know that bodies are string.
Well we'll just use type of which always trips me up that of is not capitalized.
Drives me nuts.
But ok.
If we do this you'll see that it's a string so we can't access this information it's basically coming
back like this
inside of quotes.
This whole thing.
Well that doesn't help illustrate it because it's filled with quotes but it's a giant string like this
.
And we can't access parts of that string because it's not a javascript object.
So there's a simple solution but it kind of annoying when we forget about it and you're wondering why
am I getting undefined.
There's definitely no data in this body because we're seeing it here.
Well we need to turn into an object and it's really simple to do so.
All we need to do is parse it and Javascript comes with a nice way of doing that built in looks like
this.
Jason Parrs and what we wanted to parse which would be body.
And then we'll save that result variable.
So we'll just call it data or let's call it parsed data just to make it clear.
So var parsed data because Jaisalmer up parse body.
Now if we cancel that log body it shouldn't look any different at all.
Try that.
Oh well that's silly him printing out party let's print out parsed data.
And now if we run it
we get a nicer formatted version so I lied it actually does look different.
But the data is the same itself.
So rather than being printed like a string it's printed it's printed like an object and you can see
it's structured nicely we can see at least one layer in.
So queery and then count.
So let's try doing queery save great then count and save and we don't actually need count and of course
what we need is results.
Try running not.
And then we want Channel Astronomie sunset this giant thing here.
And this is part of working with API is having to kind of drill down through the data and figure out
how something is structured which is where something like this is really useful especially with a really
long thing to chase on.
This is nice and simple.
OK.
So let's see what we get now.
Those are some sad time.
So we could do something nice and simple just like cancel that log sunset.
And why is it.
And it's coming from the API.
And you know if you're typing this and following along in six months that change that time should definitely
change.
So that's again a simple application of an API.
But it's coming from an API we're working with kind of this narly nested bit of data.
And like I said this one's pretty simple actually the API response but even then we stuff a lot of data
to kind of drill drill down through these different levels.
The key takeaway though is this based on our parts.
So when we get the data back even though it looks like the data that contains information that we want
we can't actually extract anything from it because it's all in one string.
So we're then going to turn it from the string version that looks like a javascript object just with
quotes around it to an actual javascript object that we can access and manipulate which is the magic
from that one line right there.
All right.
So next up we're going to build a simple express app that uses the request module here to actually get
information about movies and can make a search app where you can search for different movie titles and
see results using a movie API.
OK.