Transcript from the "API Handlers for POST" Lesson
>> So the next example is a post so I'm going to create another file is going to be our last.go file of this course. Post.go and in post.go, of course, we are in the same package API. I'm going to create another function post with the same signature for an HTTP handler.
[00:00:21] So it's going to be W of HTTP response writer. And the request as HTTP request pointer, it's a pointer to it. In this case, I want to make sure that it's really a post. So for that their request has a method. So it's simple and we can check if it's post, get, delete or the one that we want.
[00:00:49] For the methods, we can use a string or we can check within the constants in http.MethodPost, for example. If it's not a post, we can just right now throw an error over HTTP. We are passing the writer The message like Unsupported Method. The HTP status code that I want is probably one that is known as MethodNotAllowed.
[00:01:21] So in this URL, we are not allowing that method. For example, you want to delete? No, it's not allowed So what's my point? What I want is to receive a new activity vision and they want to save that exhibition in my collection that in my case it's going to be an in-memory collection.
[00:01:49] Later You can move this into a real database or something like that. So for that we need to remember that our collection is currently private, so I won't be able to add something to it. So I need to add a new function for adding a collection, exhibition.go within data and I'm going to add one exhibition.
[00:02:14] So I receive an exhibition. Okay, and I'm going to add it, how to add something to a slice. Does anyone remember how to add an element to a collection?
>> Did you have to like call the variable then equal and then use the pass function. Not pass, very close a pen.
[00:02:41] Yeah, that's correct. So we need to call append, but append is not actually adding something to the collection. It's returning me a new collection, so I need to say that now my list will be a new list. That will be the list after appending to that list the new exhibition.
[00:03:05] Okay, makes sense? Okay, so we have that. Now we can go back to post and the next question is how to read the exhibition that is coming from the browser or from the client. Well there are many ways to get data from the client but if the data is coming in the body as JSON and that's the most common scenario then we can Then try to decode the body.
[00:03:32] So we've seen how to create a new encoder for the output. We also have a way to create a new decoder for the input. So we can receive the body as JSON and recreate an object from our side. Okay, makes sense. So for that, I'm going to call Jason new in this case decoder.
[00:03:54] And for the input we are going to take from the request the body, and we are going to ask to decode it into an any. Remember when we used the unmarshal. So I actually need to create the variable and send that variable to, in this case, the decoder and say hey, decode me that JSON into this variable.
[00:04:24] So I need to create here my new exhibition. Let's call it item or exhibition. So let's call it exhibition. It's going to be basically data.exhibition, that's the type. Then I'm going to decode into that exhibition. But we know that every time we are passing a value, that I want the function to change what do I need to do?
>> Ampersand, I need to pass the address of that variable. Make sense? And this gives me an error because maybe it's not json, maybe it's wrong, there are a lot of maybes. Moving when you are dealing with user input. So if it's not nil, so if there was an error, well, we can throw an error.
[00:05:21] The error that we're going to throw to the writer, we can take the message from the error if if you want to take the message of the error. It's .error, that's the name of the function not to a string, it's .error. That will give you the string. In this case, we can use the code that is known as bad request.
[00:05:46] So you're saying to the client, hey, your request is bad. In this case, the JSON is not what I was expecting or it's not value. And I can also return so I can get out of here and then else or without else doesn't matter. I can go and ask to add that new exhibition and then we can write something to the user, to the output.
[00:06:17] It can be a true, a false, a message saying, okay, no, just your return something to the client. It can be adjacent with a true status. Okay, or maybe it can be nothing, because you can also say an empty body just with a status code is good enough.
[00:06:35] In fact, there is a status code, a special status code for this situation. Let me see if I can find it here. It's a StatusCreated. I think it's 203 or 401, I don't remember. I can check here, 201. What you're saying, yeah, what you ask me is what's created.
[00:07:33] It's a free tool, but the free part is getting is going away. But it's still free for now where you can quickly test on API. But before doing that, I need to hook my handler to the server because I didn't do that. So I have the handler, but on my main.go, I didn't add the handler here, so I'm going to add I will call these exhibitions/new pointing to api.both.
[00:08:08] Maybe the new is the necessary it doesn't matter. Just to change the URL. Okay, does it make sense? If you look here, when we are registering the function, we don't say that we want this only for one method. It's for all the methods. There are some libraries out there that can let you or there are ways that you can hook also the method here, but for now it's like this.
[00:08:39] I'm going to restart my server I'm going back to postman. So you can see that on postman I am making a POST to local host http.3333apiexhibitionsnew with a body of type JSON with something like this. Frontend Masters rocks, the best courses on frontend, but also backend, mobile, data and more.
[00:09:03] That's the description, the image. In our case, we have also added some things here. So we have a color, a number, let's say red. And we have this is the exhibition, remember? We've changed the original data that I created currently open, so I can say currently open true, and I can try to send this.
[00:09:32] And if I send this, then the answer for was 201 Created. Can you see a status 201 Created, okay. Now, what happened? Of course it's not changing by my goal, but it should be changing if it's working, cross your fingers. If it's working, it will change my memory collection.
[00:09:58] While the server is running, it should be there. So now if I go back here and I go to localhost/template, I go to my server side version. Now, if I scroll I have front-end master rocks and you are there in the picture, okay? So it worked. So I send a post, okay and you are there.