1
00:00:00,300 --> 00:00:00,840
All right.

2
00:00:00,840 --> 00:00:01,640
Welcome back.

3
00:00:01,830 --> 00:00:09,210
So we saw how to use the request package to make requests instead of a node app or a node file in this

4
00:00:09,210 --> 00:00:10,610
case.

5
00:00:10,770 --> 00:00:14,510
We're now going to talk about how you make a request that handles Jaison.

6
00:00:14,550 --> 00:00:19,820
So we got all of this crazy aged him from read it and from Google.

7
00:00:19,860 --> 00:00:21,740
Most the time you won't be doing that.

8
00:00:21,840 --> 00:00:24,340
Most of the time you'll be requesting an API.

9
00:00:24,630 --> 00:00:27,180
So we're going to take a really simple one.

10
00:00:27,180 --> 00:00:32,500
This Yahoo weather API that we saw that's just set up to get sunset time in Hawaii.

11
00:00:33,210 --> 00:00:34,910
So I'm going to copy that you url.

12
00:00:35,160 --> 00:00:36,180
Just double check.

13
00:00:36,180 --> 00:00:38,290
This is what we get back.

14
00:00:38,860 --> 00:00:43,560
And all we want to do is make a really simple application that every time you run it it just prints

15
00:00:43,560 --> 00:00:49,060
out the time set for today or the or the next sunset in Hawaii.

16
00:00:49,650 --> 00:00:55,740
So not that exciting but nice and simple Jason that we'll handle and what we'll want to do is print

17
00:00:55,740 --> 00:00:56,650
out.

18
00:00:56,930 --> 00:01:01,390
It looks like let's see results channel Astronomie sunset.

19
00:01:01,410 --> 00:01:07,400
So need to access those four levels down to get this 6:55 p.m. string.

20
00:01:07,800 --> 00:01:10,510
And this is just like a regular javascript object.

21
00:01:10,530 --> 00:01:18,990
So let's go back and just Adeptus first thing that we'll do is just let's change the surroundings to

22
00:01:18,990 --> 00:01:20,390
match this form that we have here.

23
00:01:20,400 --> 00:01:32,300
So if no error and response status code 200 just to clean it up then I'm going to get rid of this else

24
00:01:32,300 --> 00:01:33,340
.

25
00:01:33,750 --> 00:01:34,860
And then in here.

26
00:01:35,250 --> 00:01:38,020
Well we want to do just get rid of all that

27
00:01:40,830 --> 00:01:50,340
and just do cancel the log response or conses that log body C-3 and will change the URL.

28
00:01:50,340 --> 00:01:53,400
So just start simple.

29
00:01:53,640 --> 00:01:55,000
Copy that.

30
00:01:55,290 --> 00:01:57,530
Copy that you Ereli again paste it in.

31
00:01:57,690 --> 00:01:58,580
It's gigantic.

32
00:01:58,580 --> 00:02:00,260
I know.

33
00:02:00,570 --> 00:02:05,280
Just while we're looking at this you can see there's all these percent.

34
00:02:05,280 --> 00:02:11,260
Those are how you encode a space and you are all just in case you weren't familiar with that.

35
00:02:11,380 --> 00:02:13,570
There's this giant statement here.

36
00:02:13,680 --> 00:02:15,560
It's pretty intense.

37
00:02:15,570 --> 00:02:20,730
We're not going to focus on that if you wanted to use this API for something on your own.

38
00:02:20,730 --> 00:02:23,100
You could definitely check it out but you can see here.

39
00:02:23,160 --> 00:02:29,880
There's Maui why I'm sure we could replace this with some other location like in Alaska and we would

40
00:02:29,880 --> 00:02:32,900
expect sunset potentially to be different up north.

41
00:02:32,900 --> 00:02:34,350
I don't know.

42
00:02:34,390 --> 00:02:39,780
OK so let's just see what we get when we print out the body and let's run this picture.

43
00:02:39,780 --> 00:02:42,250
Other stuff is coming out.

44
00:02:42,630 --> 00:02:44,410
Node first request.

45
00:02:44,450 --> 00:02:45,360
Yes.

46
00:02:45,790 --> 00:02:46,310
OK.

47
00:02:46,350 --> 00:02:48,130
So we get this printed out.

48
00:02:48,780 --> 00:02:57,830
So all we really need to do is access queery and then we need to go access results.

49
00:02:57,880 --> 00:02:58,990
It's kind of hard to see here.

50
00:02:59,010 --> 00:03:00,550
Channel astronomy sunset.

51
00:03:00,600 --> 00:03:06,330
But again if you look at it here the way it's structured and this is just a nice kind of tool they use

52
00:03:06,330 --> 00:03:09,580
to help people understand what the response looks like.

53
00:03:09,750 --> 00:03:15,780
Most AP guys won't have the exact format where you can view it but of course you can just make the request

54
00:03:15,780 --> 00:03:19,890
here in your browser and take a look especially if you have just one view.

55
00:03:19,890 --> 00:03:22,560
So I'd like to do this query.

56
00:03:22,980 --> 00:03:23,370
OK.

57
00:03:23,370 --> 00:03:26,590
So that's the first level and then the next thing is results.

58
00:03:26,610 --> 00:03:28,790
So let's just start with queery.

59
00:03:29,010 --> 00:03:38,410
So if we go back we should just be able to do cancel that log body query just like that save it.

60
00:03:38,670 --> 00:03:40,530
Let's try it.

61
00:03:41,580 --> 00:03:43,400
Oh no undefined.

62
00:03:43,560 --> 00:03:44,860
Why would that be happening.

63
00:03:45,000 --> 00:03:46,120
So what's going on.

64
00:03:46,470 --> 00:03:57,330
Well the answer is that when we just get the body back here it looks like it's a javascript object but

65
00:03:57,330 --> 00:03:57,990
it's not.

66
00:03:58,170 --> 00:04:00,190
It's a string.

67
00:04:00,210 --> 00:04:04,470
So take a moment and think about how we could prove that this is a string.

68
00:04:04,730 --> 00:04:08,630
Constable Lague body how do we know that bodies are string.

69
00:04:09,180 --> 00:04:15,330
Well we'll just use type of which always trips me up that of is not capitalized.

70
00:04:15,330 --> 00:04:15,950
Drives me nuts.

71
00:04:15,950 --> 00:04:17,520
But ok.

72
00:04:18,120 --> 00:04:27,570
If we do this you'll see that it's a string so we can't access this information it's basically coming

73
00:04:27,570 --> 00:04:28,380
back like this

74
00:04:30,940 --> 00:04:32,010
inside of quotes.

75
00:04:32,010 --> 00:04:33,050
This whole thing.

76
00:04:33,180 --> 00:04:38,550
Well that doesn't help illustrate it because it's filled with quotes but it's a giant string like this

77
00:04:38,550 --> 00:04:39,420
.

78
00:04:39,420 --> 00:04:44,660
And we can't access parts of that string because it's not a javascript object.

79
00:04:44,910 --> 00:04:51,180
So there's a simple solution but it kind of annoying when we forget about it and you're wondering why

80
00:04:51,180 --> 00:04:52,710
am I getting undefined.

81
00:04:52,710 --> 00:04:58,400
There's definitely no data in this body because we're seeing it here.

82
00:04:58,710 --> 00:05:02,290
Well we need to turn into an object and it's really simple to do so.

83
00:05:02,910 --> 00:05:07,770
All we need to do is parse it and Javascript comes with a nice way of doing that built in looks like

84
00:05:07,770 --> 00:05:08,460
this.

85
00:05:08,460 --> 00:05:13,500
Jason Parrs and what we wanted to parse which would be body.

86
00:05:13,830 --> 00:05:15,800
And then we'll save that result variable.

87
00:05:15,810 --> 00:05:22,490
So we'll just call it data or let's call it parsed data just to make it clear.

88
00:05:23,160 --> 00:05:26,790
So var parsed data because Jaisalmer up parse body.

89
00:05:26,790 --> 00:05:33,370
Now if we cancel that log body it shouldn't look any different at all.

90
00:05:33,380 --> 00:05:33,810
Try that.

91
00:05:33,840 --> 00:05:39,960
Oh well that's silly him printing out party let's print out parsed data.

92
00:05:40,350 --> 00:05:41,520
And now if we run it

93
00:05:44,550 --> 00:05:48,900
we get a nicer formatted version so I lied it actually does look different.

94
00:05:49,260 --> 00:05:51,630
But the data is the same itself.

95
00:05:51,630 --> 00:05:56,250
So rather than being printed like a string it's printed it's printed like an object and you can see

96
00:05:56,730 --> 00:06:00,250
it's structured nicely we can see at least one layer in.

97
00:06:00,420 --> 00:06:02,190
So queery and then count.

98
00:06:02,190 --> 00:06:18,270
So let's try doing queery save great then count and save and we don't actually need count and of course

99
00:06:19,260 --> 00:06:24,780
what we need is results.

100
00:06:25,440 --> 00:06:26,720
Try running not.

101
00:06:27,570 --> 00:06:32,940
And then we want Channel Astronomie sunset this giant thing here.

102
00:06:34,560 --> 00:06:42,990
And this is part of working with API is having to kind of drill down through the data and figure out

103
00:06:43,050 --> 00:06:48,360
how something is structured which is where something like this is really useful especially with a really

104
00:06:48,360 --> 00:06:50,540
long thing to chase on.

105
00:06:50,640 --> 00:06:51,910
This is nice and simple.

106
00:06:52,170 --> 00:06:52,650
OK.

107
00:06:52,650 --> 00:06:54,160
So let's see what we get now.

108
00:06:54,210 --> 00:06:55,610
Those are some sad time.

109
00:06:55,740 --> 00:07:02,680
So we could do something nice and simple just like cancel that log sunset.

110
00:07:03,110 --> 00:07:08,700
And why is it.

111
00:07:10,470 --> 00:07:12,150
And it's coming from the API.

112
00:07:12,330 --> 00:07:18,420
And you know if you're typing this and following along in six months that change that time should definitely

113
00:07:18,420 --> 00:07:20,060
change.

114
00:07:20,070 --> 00:07:24,110
So that's again a simple application of an API.

115
00:07:24,630 --> 00:07:30,890
But it's coming from an API we're working with kind of this narly nested bit of data.

116
00:07:31,230 --> 00:07:37,440
And like I said this one's pretty simple actually the API response but even then we stuff a lot of data

117
00:07:37,500 --> 00:07:40,790
to kind of drill drill down through these different levels.

118
00:07:40,830 --> 00:07:43,460
The key takeaway though is this based on our parts.

119
00:07:43,500 --> 00:07:48,660
So when we get the data back even though it looks like the data that contains information that we want

120
00:07:49,140 --> 00:07:53,090
we can't actually extract anything from it because it's all in one string.

121
00:07:53,340 --> 00:07:59,190
So we're then going to turn it from the string version that looks like a javascript object just with

122
00:07:59,190 --> 00:08:05,550
quotes around it to an actual javascript object that we can access and manipulate which is the magic

123
00:08:05,730 --> 00:08:07,570
from that one line right there.

124
00:08:07,950 --> 00:08:08,370
All right.

125
00:08:08,370 --> 00:08:15,030
So next up we're going to build a simple express app that uses the request module here to actually get

126
00:08:15,030 --> 00:08:20,370
information about movies and can make a search app where you can search for different movie titles and

127
00:08:20,370 --> 00:08:22,400
see results using a movie API.

128
00:08:22,720 --> 00:08:23,280
OK.
