1
00:00:00,500 --> 00:00:06,380
So this time around, we want to change this button, which currently just displays a search form to

2
00:00:06,380 --> 00:00:07,980
search for availability.

3
00:00:08,390 --> 00:00:15,500
We want to change that so that it behind the scenes sends a request to the server to check for availability.

4
00:00:15,950 --> 00:00:19,070
And the server will send back a Jason response.

5
00:00:19,070 --> 00:00:21,140
And Jason will talk about more in just a moment.

6
00:00:21,470 --> 00:00:24,210
But it stands for JavaScript object notation.

7
00:00:24,740 --> 00:00:27,620
We wanted to do that without ever leaving this page.

8
00:00:27,620 --> 00:00:31,110
So we don't have to do a full page reload, which will make things ever so much faster.

9
00:00:31,700 --> 00:00:34,220
So we're going to do this in a series of steps.

10
00:00:34,220 --> 00:00:39,800
And the first step, which will cover in this lecture is actually to create a handler that this form

11
00:00:39,800 --> 00:00:40,270
will call.

12
00:00:40,280 --> 00:00:42,690
We're not going to call that until the next lecture, but we're going to call it.

13
00:00:43,280 --> 00:00:45,710
So first of all, let's think about what the what we're going to do.

14
00:00:45,710 --> 00:00:51,230
We want to call this form and we want it to send back a response in the form of Jason.

15
00:00:51,920 --> 00:00:55,190
Now, you may never have encountered Jason, and it's not that difficult.

16
00:00:55,640 --> 00:01:00,440
Here's an example of a JSON file, and this is exactly the format I want to send back.

17
00:01:00,890 --> 00:01:04,730
So Jason is wrapped in curly brackets like this.

18
00:01:05,000 --> 00:01:10,970
And here is the parameter name or the value name, and here's the value that's that's associated with

19
00:01:10,970 --> 00:01:11,380
OK.

20
00:01:11,810 --> 00:01:17,570
So obviously if we send a request to search for dates, that handler should take the dates, should

21
00:01:17,570 --> 00:01:21,800
look at them and decide whether or not the room is available on the dates in question.

22
00:01:22,010 --> 00:01:28,220
If it is available, it should set OK to true and then give a message like available or if they're not

23
00:01:28,220 --> 00:01:32,420
available it should say OK is equal to false and send a message not available.

24
00:01:32,600 --> 00:01:34,310
And that's just for an example.

25
00:01:34,310 --> 00:01:40,670
If we wanted to have a third parameter like something else, well, we just give it a name followed

26
00:01:40,670 --> 00:01:41,480
by a colon.

27
00:01:41,480 --> 00:01:44,840
The name is in quotation marks and then some value, which might be 100 in this case.

28
00:01:44,840 --> 00:01:45,670
But we're not going to do that.

29
00:01:45,980 --> 00:01:50,060
We're going to go with a straight to to the values that we have to set here.

30
00:01:50,490 --> 00:01:50,860
All right.

31
00:01:50,870 --> 00:01:55,970
So let's go back to our code, which is way over here and go and let's stop the application if it's

32
00:01:55,970 --> 00:01:58,400
running and we're going to create a new handler.

33
00:01:58,490 --> 00:02:03,500
So I'll create the handler first and I'm going to give this a name and I'll put it near post availability.

34
00:02:03,500 --> 00:02:09,170
Now, just for the purposes of this exercise, I'm going to make this handler, which I'm going to call

35
00:02:09,290 --> 00:02:17,270
availability, JSON like that, and then I'll change my comment and change my comment here to say that

36
00:02:17,270 --> 00:02:25,550
it handles request for availability and sends Jason response.

37
00:02:26,120 --> 00:02:28,880
OK, and I don't want to do any of this stuff in here.

38
00:02:29,480 --> 00:02:30,440
So there's my handler.

39
00:02:30,620 --> 00:02:32,300
OK, and I better spell request.

40
00:02:32,300 --> 00:02:32,540
Right.

41
00:02:33,740 --> 00:02:34,710
So that's my name.

42
00:02:34,760 --> 00:02:39,170
Let's go back to our roots file and create a route to that which I'll put right here and I'm going to

43
00:02:39,170 --> 00:02:43,420
put these all on there and online through my own block just so things are a little clearer.

44
00:02:44,270 --> 00:02:49,070
So we have this one that I just duplicated is going to be calling a different U.

45
00:02:49,070 --> 00:02:50,480
URL, obviously.

46
00:02:50,480 --> 00:02:52,160
Otherwise it will never match this route.

47
00:02:52,760 --> 00:02:53,810
And I'll call this Jason.

48
00:02:54,500 --> 00:02:57,230
And the handler is going to call is availability Jason.

49
00:02:57,500 --> 00:03:01,070
So I now have a link between what I'm going to make this a get for right now.

50
00:03:01,070 --> 00:03:08,090
We'll change it back to post later when someone makes a get request to this URL slash search dash availability

51
00:03:08,090 --> 00:03:12,140
destination, it calls this handler and I'll get there right here.

52
00:03:13,100 --> 00:03:14,180
Now, what am I going to do in here?

53
00:03:14,300 --> 00:03:16,310
I don't want to build a Web page.

54
00:03:16,340 --> 00:03:18,200
I can't send back straight text.

55
00:03:18,350 --> 00:03:21,080
I actually want to build a Jason request.

56
00:03:21,560 --> 00:03:28,460
And the nice thing about this is that Go makes it remarkably easy to generate JSON and we do it by defining

57
00:03:28,460 --> 00:03:31,000
our own type or using a type if we haven't already.

58
00:03:31,010 --> 00:03:36,560
I want to call this JSON response and I'm going to leave it lowercase because I'm never going to use

59
00:03:36,560 --> 00:03:39,290
this outside of this package, the handler's package.

60
00:03:39,650 --> 00:03:41,390
So it's a JSON response.

61
00:03:41,390 --> 00:03:42,200
It is a struct.

62
00:03:42,200 --> 00:03:44,740
And we just describe what we want our Jason to look like.

63
00:03:45,350 --> 00:03:50,150
Now, if you go back to that Jason file that I was showing you a minute ago, we have two values, OK?

64
00:03:50,150 --> 00:03:50,870
And message.

65
00:03:51,200 --> 00:03:57,470
Well, we can't use those as names because one of the rules, if you want to export a Jason a struct

66
00:03:57,470 --> 00:04:01,450
to JSON is that the member names have to start with a capital letter.

67
00:04:01,460 --> 00:04:03,200
They have to be exported, in other words.

68
00:04:03,350 --> 00:04:04,550
So I'll call mine, OK?

69
00:04:04,970 --> 00:04:11,540
And it's going to be of type bool for Boolean and then the next one is actually message.

70
00:04:11,570 --> 00:04:14,690
So I'll just call that message and that's just going to hold a string.

71
00:04:15,500 --> 00:04:17,240
Now that is not enough.

72
00:04:17,240 --> 00:04:18,380
Technically speaking it is.

73
00:04:18,380 --> 00:04:25,190
But you need to get in the habit of actually explicitly telling go what you want to use as the values

74
00:04:25,190 --> 00:04:26,390
in the JSON response.

75
00:04:27,080 --> 00:04:32,570
Any time that I'm populating the value of OK, I want it to return in JSON.

76
00:04:32,570 --> 00:04:34,850
So I've got back to around all of this.

77
00:04:35,210 --> 00:04:42,380
That's the little button just above the tab on my keyboard, subarctic gesso and to say for JSON, then

78
00:04:42,380 --> 00:04:47,660
a colon, then in double quotes what you want the value to be called or that feel to be called in JSON.

79
00:04:47,660 --> 00:04:54,680
I don't want it to be called OC lowercase the same way for message for Jason Colon, in quotes I wanted

80
00:04:54,680 --> 00:04:56,180
to call message all lowercase.

81
00:04:56,690 --> 00:04:59,930
Now if I don't specify, these go will do its best.

82
00:05:00,340 --> 00:05:04,690
Well, that probably should be a lower case, OK, this probably should be a lower case message, but

83
00:05:04,690 --> 00:05:05,560
I'm not going to do that.

84
00:05:05,570 --> 00:05:08,380
I'm not going to trust it because I'm not sure that's going to stay there.

85
00:05:08,980 --> 00:05:10,180
Maybe five years from now.

86
00:05:10,180 --> 00:05:12,970
The go authors will decide to change the way that they're doing that.

87
00:05:13,270 --> 00:05:15,570
It's unlikely, but why take the chance?

88
00:05:15,580 --> 00:05:17,610
It's very easy to specify those right now.

89
00:05:18,190 --> 00:05:25,510
So I've created a struct type called JSON Response, and the convention engo is to when you're creating

90
00:05:25,510 --> 00:05:29,860
a struct like this or a type like this that you're only going to use in a couple of places, put it

91
00:05:29,860 --> 00:05:32,580
as close as possible to the code that uses it.

92
00:05:32,620 --> 00:05:33,620
That way, it's easy to find.

93
00:05:34,180 --> 00:05:35,620
So in my function, what am I going to do?

94
00:05:35,640 --> 00:05:37,840
Well, right now I want to define a response.

95
00:05:37,840 --> 00:05:39,670
What am I going to send back when I say response?

96
00:05:39,670 --> 00:05:41,170
I'm not talking about response writer.

97
00:05:41,470 --> 00:05:44,020
I'm talking about what I'm going to push to that response.

98
00:05:44,020 --> 00:05:44,200
Right.

99
00:05:44,290 --> 00:05:51,970
So it's not are I'll just call it resp and I will assign that the value of type JSON response and I'm

100
00:05:51,970 --> 00:05:55,180
going to use the shorthand to create and populate the struct at once.

101
00:05:55,630 --> 00:06:01,840
Jason responses my type and in curly brackets I'm going to say, OK, true.

102
00:06:01,840 --> 00:06:11,440
Right now I'm going to set my message to a male with an exclamation mark and a format that good.

103
00:06:11,440 --> 00:06:12,960
OK, so I've created that value.

104
00:06:13,390 --> 00:06:16,810
Now I need to convert that to Jason and send it back.

105
00:06:16,810 --> 00:06:19,120
And here's the the magic that is go.

106
00:06:19,900 --> 00:06:22,090
I'm going to create a response from that.

107
00:06:22,090 --> 00:06:26,710
I'm going to call the response out and it might create an error when I try to create this.

108
00:06:26,710 --> 00:06:30,430
So I'll trap for an error because this is what I'm going to call actually returns to values.

109
00:06:30,670 --> 00:06:31,660
And you'll see that in a moment.

110
00:06:32,050 --> 00:06:35,920
And I'm going to call the built in package that's part of the standard library called Jason.

111
00:06:36,790 --> 00:06:44,080
And in JSON, I'm going to call the method mercial, but I'm going to call the method Marshall the method

112
00:06:44,080 --> 00:06:46,870
mercial indent, which is a little more complex.

113
00:06:46,870 --> 00:06:49,240
I just want to format things so that we can see them properly.

114
00:06:49,240 --> 00:06:52,180
And as you can see here, this takes an interface.

115
00:06:52,180 --> 00:06:53,290
So it takes some value.

116
00:06:53,290 --> 00:06:58,240
Well, my struct is an interface because everything's at least the interface can go a prefix which I'll

117
00:06:58,240 --> 00:07:02,200
leave empty and whether or not what I'm going to indented by just to make it readable.

118
00:07:02,830 --> 00:07:07,360
So I'll choose Mercial indent and I'm going to pass it out.

119
00:07:07,900 --> 00:07:12,820
No prefix and I want to indented by one, two, three, four, five spaces.

120
00:07:13,190 --> 00:07:18,160
OK then of course because I'm possibly throwing an error, I should check for that.

121
00:07:18,160 --> 00:07:22,270
So I'll see if error is not equal to nil then do something.

122
00:07:22,270 --> 00:07:23,200
And what do I want to do.

123
00:07:23,200 --> 00:07:26,890
I'll just log the print line error which is sufficient for our purposes right now.

124
00:07:27,760 --> 00:07:29,380
Otherwise what am I going to do?

125
00:07:29,380 --> 00:07:34,930
I am going to send my my information directly to the response writer and we did that already.

126
00:07:35,440 --> 00:07:44,980
We can just call the W don't write and pass it out because out is already not out here.

127
00:07:44,980 --> 00:07:47,350
This should be resp because it doesn't exist.

128
00:07:48,220 --> 00:07:49,480
Out is already.

129
00:07:49,570 --> 00:07:52,150
If you look at this, Marshall Indebt throws back.

130
00:07:53,560 --> 00:07:59,500
A slice of bites, and that's exactly what we need to pass to the writer, that's almost enough and

131
00:07:59,500 --> 00:08:02,130
chances are that'll work in the vast majority of cases.

132
00:08:02,500 --> 00:08:09,130
But the last thing I want to do is I need to create a header that tells the Web browser that's receiving

133
00:08:09,130 --> 00:08:09,880
my response.

134
00:08:10,030 --> 00:08:11,710
What kind of response?

135
00:08:11,710 --> 00:08:13,200
I'm sending it now.

136
00:08:13,270 --> 00:08:17,560
Whenever whenever you call a Web page, you are automatically getting a header sent back to you by the

137
00:08:17,560 --> 00:08:20,380
server that says this is of typed text HTML.

138
00:08:20,740 --> 00:08:27,190
But I want to send a type of application JSON, which is the standard header for JSON files, and I

139
00:08:27,190 --> 00:08:34,990
just do that before I call my write, I call W header and that has a method called set and I'm going

140
00:08:34,990 --> 00:08:36,360
to set that to two things.

141
00:08:36,370 --> 00:08:38,700
The first thing is what am I sending and what kind of header is this?

142
00:08:38,710 --> 00:08:42,230
It has to be exactly like this content dash type.

143
00:08:42,760 --> 00:08:45,460
So this is telling the browser, here's the kind of content you're going to get.

144
00:08:45,670 --> 00:08:48,610
And the second argument is just application driessen.

145
00:08:49,570 --> 00:08:55,510
So when I do this, when I call this now, because if you look back at my roots file, if I call this

146
00:08:55,510 --> 00:08:58,960
using a get request, which will change to post before too long.

147
00:08:58,960 --> 00:09:00,610
But right now I just want to make sure it works.

148
00:09:02,080 --> 00:09:05,770
This route matches this path or this this handler.

149
00:09:05,920 --> 00:09:11,170
And all that does is say here's my JSON type that I'm going to be using this way to find a struct type

150
00:09:11,170 --> 00:09:12,430
that I'm going to be using shortly.

151
00:09:12,790 --> 00:09:17,590
I create that struct type in a variable called response.

152
00:09:17,800 --> 00:09:21,790
It has OK for its first member, it has a message for a second member.

153
00:09:22,120 --> 00:09:24,050
Then I say, Marschall this to Jason.

154
00:09:24,280 --> 00:09:29,230
So this intenta this Marschall and it actually looks at the struct type for this and says, do you have

155
00:09:29,230 --> 00:09:31,420
any tags up here that tell me what to call those fields?

156
00:09:31,420 --> 00:09:31,950
And Jason.

157
00:09:31,960 --> 00:09:32,800
Oh yes you do.

158
00:09:33,280 --> 00:09:35,260
All right then it creates that.

159
00:09:35,260 --> 00:09:41,560
Then I write that result as a content type application, JSON right to the Web browser.

160
00:09:41,590 --> 00:09:43,690
So let's say let's put one more thing in here as well.

161
00:09:44,060 --> 00:09:52,600
We'll just write a log print line, log print line and I will print out the value of about now.

162
00:09:52,600 --> 00:09:58,060
I could pass it bytes, but if you actually do that, you'll see a whole bunch of codes on your terminal

163
00:09:58,060 --> 00:09:59,650
window that aren't going to be meaningful.

164
00:09:59,960 --> 00:10:04,460
So I'm going to cast this to type string just so it looks quite readable.

165
00:10:04,840 --> 00:10:05,280
All right.

166
00:10:05,860 --> 00:10:07,630
So let's start our application.

167
00:10:09,160 --> 00:10:11,160
Go run command slash web slash.

168
00:10:11,170 --> 00:10:11,830
Start out, go.

169
00:10:14,010 --> 00:10:20,820
Everything compiled, let's go over to our Web browser and let's change the URL up here from what it

170
00:10:20,820 --> 00:10:23,220
is right now to what do we call it in the rules file?

171
00:10:23,250 --> 00:10:28,750
I better go make sure in route's I called it search availability JSON.

172
00:10:29,070 --> 00:10:33,890
I saw a copy, that whole thing, including the slash, go back to my web browser wherever it is.

173
00:10:33,900 --> 00:10:34,470
There it is.

174
00:10:35,850 --> 00:10:39,690
And paste it in here and see what we get.

175
00:10:40,290 --> 00:10:45,280
And what I get in Firefox is I get a nice rendering of what the JSON looks like.

176
00:10:45,300 --> 00:10:49,830
So this says here the little tab in Firefox, Chrome is probably a little different and it shows me

177
00:10:49,830 --> 00:10:52,140
OK is such a true and message it set available.

178
00:10:52,150 --> 00:10:55,650
I can look at the raw data and that will show me the actual message itself.

179
00:10:55,920 --> 00:10:57,720
OK, true message available.

180
00:10:57,720 --> 00:10:58,230
Perfect.

181
00:10:58,590 --> 00:11:04,470
And the headers are I look at that, it says the header types are the response headers are application

182
00:11:04,470 --> 00:11:05,070
JSON.

183
00:11:05,070 --> 00:11:07,680
So everything worked exactly as it should.

184
00:11:07,920 --> 00:11:12,410
And if I go back to my go terminal, I wrote that to the terminal window.

185
00:11:12,510 --> 00:11:14,810
So everything seems to work absolutely beautiful.

186
00:11:15,630 --> 00:11:23,000
Now, if I stop the application and go back to my handler and change that defaults wherever it is where

187
00:11:23,020 --> 00:11:30,390
my handlers, their handlers change this from true to false, start the application back up.

188
00:11:32,970 --> 00:11:38,070
And go back to my terminal or my Web browser and reload it now, it should give me the same header file,

189
00:11:38,220 --> 00:11:42,190
but it should give OK thought, OK, this works exactly the way that I want it to.

190
00:11:42,750 --> 00:11:48,870
So in the next lecture, we'll turn this back into a post request and we'll actually build the JavaScript

191
00:11:48,870 --> 00:11:57,990
necessary to send an example or an Ajax request from our client, the Web browser to the server and

192
00:11:57,990 --> 00:12:02,160
listen for the response and take some action based upon the response we get from the server.

193
00:12:02,430 --> 00:12:06,110
And we'll take care of that in the next lecture or possibly next to lectures, but we'll get it done.
