Build a Fullstack App with Vanilla JS and Go

Creating JSON Route Handler

Maximiliano Firtman
Independent Consultant
Build a Fullstack App with Vanilla JS and Go

Lesson Description

The "Creating JSON Route Handler" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to create a handler for a RESTful API in Go. He creates a method within the movie handler structure to handle the API request for retrieving the top movies. He also demonstrates how to set the content type header to JSON and encode the movie data into JSON format.

Preview
Close

Transcript from the "Creating JSON Route Handler" Lesson

[00:00:00]
>> Maximiliano Flirtman: So far, the only thing that we have is the model here. Why are we creating the model? Because we want the handler to actually grab from a URL, grab the movies and download the movies in JSON. Okay, so we are we are now coding the back In, and the whole idea of making a model is that we want to have an API, a Restful API, that can let us take the movies from the server.

[00:00:32]
And the server is taking that from database. For that, we use handlers. So we need a handler. So far, we are using the server only to serve static files. We need to move one step further and actually create a handler for, for example, movies. Let's start with movies, and then we do the others.

[00:00:56]
So where are the handlers right now? They are at main.go. In our main.go I have only one handler. This is our handler for static files, okay? The frontend. So now we need another handler to actually do something else. But for that, instead of writing everything here as a function, maybe it's a good idea to create something like the file server.

[00:01:30]
The file server has its own I mean, it's from the from go, from the standard library. It's actually doing its stuff somewhere else. So something similar when you hear. So I'm going to create another package for my handlers. Remember, what's a handler? It's a value that will handle one particular type of URL, in this case, the API for movies, okay?

[00:01:57]
So my goal is something like this. I wanna type localhost 8080/api/movies/,top. And then when I get all the top movies, okay, of course, now it's 404. So my goal is to see a JSON file there with the top movies from the database. So I need a handler, and we're going to write that handler directly in another value, okay?

[00:02:25]
So that's why I'm creating a package for a folder for my handlers, and within that package I'm going to create a movie, can be handler or handlers.go, it depends. Why? Because we can use the same file to write different handlers because maybe we have the top movies, we have another handler for one movie, so give me the digits of only one movie and so on.

[00:02:55]
So we create package handlers.
>> Maximiliano Flirtman: And the whole idea here is that I'm going to create a type movie handler. There is an instructor. For now, I will keep that empty. Remember, why are we doing this? This is how you create something similar to object, you can go.

[00:03:20]
So I'm creating a movie handler object. Let's say it's called value, it's a structure but let's say object for now, okay? It's pretty bad that term from a goal point of view, okay? We don't use the idea of objects but anyway. So then, do you remember how to create methods?

[00:03:42]
How to create methods in structures, in Go? Does anyone remember?
>> Speaker 2: You wrapped the handler before the function name.
>> Maximiliano Flirtman: So it's a function. So we create a function, okay? For example, we can create the function GetTopMovies, okay, by the way, handlers, every HTTP handler, has the same signature.

[00:04:09]
It has an HTTP.Rsponsewriter and an HTTP.Request that is actually a pointer, oops. What is this? Well, the request is the incoming data that is coming from from the from the client. So you have the user agent, the headers, what's the URL? Cookies, everything that the client is sending.

[00:04:37]
And we use the writer to send data back to the client. So we get the data, we send data back, that's how that works. But this is just a public function in a package, it's not a method. As he was mentioning, to actually make it a method of my movie handler, I need I have this special syntax of adding here, let's call it h, a pointer to a movie handler.

[00:05:11]
So now this is my signature, okay? Yes.
>> Speaker 3: Why the Pointer and HTTP request.
>> Maximiliano Flirtman: This is how that works. I mean, this is how HTTP handler works. So let me explain that in a second when we go back here to the main when we are sending handle, handle has the the pattern and then a handler that come from HTTP handler.

[00:05:43]
When we look at that HTTP handler, see if I can get into, it has that interface. So the quick answer is, because that's the interface that we must match. So if not, it's not gonna work, okay? The only way to create a handler is to follow that interface, and that interface is receiving a pointer to their request.

[00:06:08]
The whole deal here is that Go or, I mean, the developers of Go, who coded that part, they didn't want to send you a copy, a clone of the request. Because if there is no pointer here, when they're sending you a request, it's creating a clone, another copy.

[00:06:28]
And it doesn't make sense, so they're sending the argument as a pointer, as a reference, and not as a value. Does it make sense? That's why? So the short answer is that it's mandatory. Okay, so that's why we have to follow exactly that interface. So that's the interface that I'm following here, okay?

[00:06:51]
So what's the whole idea where, for now, I will just instead of going to the database, let's try to test this step by step, like baby steps. And we're going to create a list of movies here. So I'm going to create an array of models.movie. Remember you create things like this with curly braces, and then I can create a movie with an ID one TMDB.

[00:07:32]
I'm going to just fake, you have one to copy and paste if you want from with me, but you got the idea, right? Remember this is the automatic initializer that we have available for this. We have release here, the genres it's another collection. Remember to use here, Cali braces.

[00:07:59]
We have an ID and a name
>> Maximiliano Flirtman: And so on. We have keywords,
>> Maximiliano Flirtman: We can add keywords or not. And we have the Castings, it's a collection of actors. So, we can create an actor, just one, name Max. I'm an actor for the movie, okay? So this is one movie and of course I can another movie just for dummy data to do a baby step.

[00:08:34]
I will just change this back to the future. Think it was 90, I think it was 84 the release year, not the year where the movies, but 84 or 85 doesn't matter, okay? So I have that array of movies and what I need, I wanna write this, okay?

[00:08:55]
So I wanna write this to the output. I wanna send this JSON to the output. Who is the output, the client? Who is the client? In this case, my browser. Okay, so I want to see that in JSON format, okay? That's like my my first step, to send JSON, I need to say so, how?

[00:09:21]
I need to talk to the writer. Remember the writer is this argument W and I need to talk to the headers. I need to set the header. And the header this is HTTP spec content type. And the mini type is application/json. This is how the browser will know or the client, it can be the browser, it can be my app, will know that we are actually sending JSON, okay?

[00:09:52]
That's the first step. Then we need to encode our model, our value, the collection of two movies that I have, or three movies that I have into JSON. Fortunately, we have JSON compatibility in Go, a standard library, okay? We can talk to JSON, the JSON package. And what we're going to do with JSON is create a new encoder over the writer.

[00:10:26]
So we're going to say, hey, what I want is to send JSON to the output. Does that make sense? So I don't need the JSON as a string first, I don't wanna look at the string. I just wanna, once you are creating a JSON, just send it directly to the output, so to the writer, and that's the new encoder, and we're going to encode the data.

[00:10:52]
The data is actually movies, okay? So I'm sending movies. The problem is that this can lead to an error, okay? So it can return an error. So what happens? Maybe because it couldn't convert that into JSON, something was wrong, something happened, okay? So that's the moment where I need to do something else.

[00:11:22]
The most common way to solve this problem in Go is to ask for this. I'm not sure if you have seen this in Go when you're using if conditionals, you can have more than one expression. In that case, the last one is the condition, the Boolean condition. For example, in this case, I can just go and say if the error is not nil, that means there was an error a problem, okay?

[00:11:49]
We will load this, okay? Something like that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now