So this time around, we want to change this button, which currently just displays a search form to
search for availability.
We want to change that so that it behind the scenes sends a request to the server to check for availability.
And the server will send back a Jason response.
And Jason will talk about more in just a moment.
But it stands for JavaScript object notation.
We wanted to do that without ever leaving this page.
So we don't have to do a full page reload, which will make things ever so much faster.
So we're going to do this in a series of steps.
And the first step, which will cover in this lecture is actually to create a handler that this form
will call.
We're not going to call that until the next lecture, but we're going to call it.
So first of all, let's think about what the what we're going to do.
We want to call this form and we want it to send back a response in the form of Jason.
Now, you may never have encountered Jason, and it's not that difficult.
Here's an example of a JSON file, and this is exactly the format I want to send back.
So Jason is wrapped in curly brackets like this.
And here is the parameter name or the value name, and here's the value that's that's associated with
OK.
So obviously if we send a request to search for dates, that handler should take the dates, should
look at them and decide whether or not the room is available on the dates in question.
If it is available, it should set OK to true and then give a message like available or if they're not
available it should say OK is equal to false and send a message not available.
And that's just for an example.
If we wanted to have a third parameter like something else, well, we just give it a name followed
by a colon.
The name is in quotation marks and then some value, which might be 100 in this case.
But we're not going to do that.
We're going to go with a straight to to the values that we have to set here.
All right.
So let's go back to our code, which is way over here and go and let's stop the application if it's
running and we're going to create a new handler.
So I'll create the handler first and I'm going to give this a name and I'll put it near post availability.
Now, just for the purposes of this exercise, I'm going to make this handler, which I'm going to call
availability, JSON like that, and then I'll change my comment and change my comment here to say that
it handles request for availability and sends Jason response.
OK, and I don't want to do any of this stuff in here.
So there's my handler.
OK, and I better spell request.
Right.
So that's my name.
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
put these all on there and online through my own block just so things are a little clearer.
So we have this one that I just duplicated is going to be calling a different U.
URL, obviously.
Otherwise it will never match this route.
And I'll call this Jason.
And the handler is going to call is availability Jason.
So I now have a link between what I'm going to make this a get for right now.
We'll change it back to post later when someone makes a get request to this URL slash search dash availability
destination, it calls this handler and I'll get there right here.
Now, what am I going to do in here?
I don't want to build a Web page.
I can't send back straight text.
I actually want to build a Jason request.
And the nice thing about this is that Go makes it remarkably easy to generate JSON and we do it by defining
our own type or using a type if we haven't already.
I want to call this JSON response and I'm going to leave it lowercase because I'm never going to use
this outside of this package, the handler's package.
So it's a JSON response.
It is a struct.
And we just describe what we want our Jason to look like.
Now, if you go back to that Jason file that I was showing you a minute ago, we have two values, OK?
And message.
Well, we can't use those as names because one of the rules, if you want to export a Jason a struct
to JSON is that the member names have to start with a capital letter.
They have to be exported, in other words.
So I'll call mine, OK?
And it's going to be of type bool for Boolean and then the next one is actually message.
So I'll just call that message and that's just going to hold a string.
Now that is not enough.
Technically speaking it is.
But you need to get in the habit of actually explicitly telling go what you want to use as the values
in the JSON response.
Any time that I'm populating the value of OK, I want it to return in JSON.
So I've got back to around all of this.
That's the little button just above the tab on my keyboard, subarctic gesso and to say for JSON, then
a colon, then in double quotes what you want the value to be called or that feel to be called in JSON.
I don't want it to be called OC lowercase the same way for message for Jason Colon, in quotes I wanted
to call message all lowercase.
Now if I don't specify, these go will do its best.
Well, that probably should be a lower case, OK, this probably should be a lower case message, but
I'm not going to do that.
I'm not going to trust it because I'm not sure that's going to stay there.
Maybe five years from now.
The go authors will decide to change the way that they're doing that.
It's unlikely, but why take the chance?
It's very easy to specify those right now.
So I've created a struct type called JSON Response, and the convention engo is to when you're creating
a struct like this or a type like this that you're only going to use in a couple of places, put it
as close as possible to the code that uses it.
That way, it's easy to find.
So in my function, what am I going to do?
Well, right now I want to define a response.
What am I going to send back when I say response?
I'm not talking about response writer.
I'm talking about what I'm going to push to that response.
Right.
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
going to use the shorthand to create and populate the struct at once.
Jason responses my type and in curly brackets I'm going to say, OK, true.
Right now I'm going to set my message to a male with an exclamation mark and a format that good.
OK, so I've created that value.
Now I need to convert that to Jason and send it back.
And here's the the magic that is go.
I'm going to create a response from that.
I'm going to call the response out and it might create an error when I try to create this.
So I'll trap for an error because this is what I'm going to call actually returns to values.
And you'll see that in a moment.
And I'm going to call the built in package that's part of the standard library called Jason.
And in JSON, I'm going to call the method mercial, but I'm going to call the method Marshall the method
mercial indent, which is a little more complex.
I just want to format things so that we can see them properly.
And as you can see here, this takes an interface.
So it takes some value.
Well, my struct is an interface because everything's at least the interface can go a prefix which I'll
leave empty and whether or not what I'm going to indented by just to make it readable.
So I'll choose Mercial indent and I'm going to pass it out.
No prefix and I want to indented by one, two, three, four, five spaces.
OK then of course because I'm possibly throwing an error, I should check for that.
So I'll see if error is not equal to nil then do something.
And what do I want to do.
I'll just log the print line error which is sufficient for our purposes right now.
Otherwise what am I going to do?
I am going to send my my information directly to the response writer and we did that already.
We can just call the W don't write and pass it out because out is already not out here.
This should be resp because it doesn't exist.
Out is already.
If you look at this, Marshall Indebt throws back.
A slice of bites, and that's exactly what we need to pass to the writer, that's almost enough and
chances are that'll work in the vast majority of cases.
But the last thing I want to do is I need to create a header that tells the Web browser that's receiving
my response.
What kind of response?
I'm sending it now.
Whenever whenever you call a Web page, you are automatically getting a header sent back to you by the
server that says this is of typed text HTML.
But I want to send a type of application JSON, which is the standard header for JSON files, and I
just do that before I call my write, I call W header and that has a method called set and I'm going
to set that to two things.
The first thing is what am I sending and what kind of header is this?
It has to be exactly like this content dash type.
So this is telling the browser, here's the kind of content you're going to get.
And the second argument is just application driessen.
So when I do this, when I call this now, because if you look back at my roots file, if I call this
using a get request, which will change to post before too long.
But right now I just want to make sure it works.
This route matches this path or this this handler.
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
that I'm going to be using shortly.
I create that struct type in a variable called response.
It has OK for its first member, it has a message for a second member.
Then I say, Marschall this to Jason.
So this intenta this Marschall and it actually looks at the struct type for this and says, do you have
any tags up here that tell me what to call those fields?
And Jason.
Oh yes you do.
All right then it creates that.
Then I write that result as a content type application, JSON right to the Web browser.
So let's say let's put one more thing in here as well.
We'll just write a log print line, log print line and I will print out the value of about now.
I could pass it bytes, but if you actually do that, you'll see a whole bunch of codes on your terminal
window that aren't going to be meaningful.
So I'm going to cast this to type string just so it looks quite readable.
All right.
So let's start our application.
Go run command slash web slash.
Start out, go.
Everything compiled, let's go over to our Web browser and let's change the URL up here from what it
is right now to what do we call it in the rules file?
I better go make sure in route's I called it search availability JSON.
I saw a copy, that whole thing, including the slash, go back to my web browser wherever it is.
There it is.
And paste it in here and see what we get.
And what I get in Firefox is I get a nice rendering of what the JSON looks like.
So this says here the little tab in Firefox, Chrome is probably a little different and it shows me
OK is such a true and message it set available.
I can look at the raw data and that will show me the actual message itself.
OK, true message available.
Perfect.
And the headers are I look at that, it says the header types are the response headers are application
JSON.
So everything worked exactly as it should.
And if I go back to my go terminal, I wrote that to the terminal window.
So everything seems to work absolutely beautiful.
Now, if I stop the application and go back to my handler and change that defaults wherever it is where
my handlers, their handlers change this from true to false, start the application back up.
And go back to my terminal or my Web browser and reload it now, it should give me the same header file,
but it should give OK thought, OK, this works exactly the way that I want it to.
So in the next lecture, we'll turn this back into a post request and we'll actually build the JavaScript
necessary to send an example or an Ajax request from our client, the Web browser to the server and
listen for the response and take some action based upon the response we get from the server.
And we'll take care of that in the next lecture or possibly next to lectures, but we'll get it done.