The aim of this video is to show how to leverage HTTP verbs, status codes, and URIs to implement a REST API that offers basic CRUD functionality.
- [Instructor] Hi, in this video we will see how to implement a simple REST API. And this is what we're going to take a look at in this video. We will be using the request path and query parameters to implement our REST API We will also be using correct HTTP status codes. For our REST API, we will be using different HTTP verbs such as get, put, post, and delete, to implement the REST API that can store, update and retrieve books.
So enough talking, let's switch to code. What I've done here is already prepared the code we've been implementing throughout the last two videos. We have our simple HTTP server, and we have the means to marshal and unmarshal Go structures to and from JSON. First of all, I will add two new endpoints to our HTTP server, to store and retrieve books, and to retrieve them by ISBN. Let's do this.
So basically I've added two REST endpoints now. The first one will be used to retrieve all the books and to store books. The second one will be used to retrieve individual books by ISBN, to update them, and to delete them. This is all we have to do in the microservice. Let's go into the code. So again here, I did a little preparation already. What you see here is we have our book structure.
Again, as we learned in our previous video, we have the title, the author, the ISBN, and an optional description, and we'll use those tags here, well, to customize the JSON marshalling. Also, in here I've added the dictionary structure. The key of this dictionary is the ISBN, and the values are individual books. This is basically our in-memory data structure.
Here, you see those two handler functions we used for our REST service, and it's all about implementing those two now. Let's get started with the first one. Assuming you have a get request for api/books to return the list of books. Let's do this. And we switch on the request method.
And in case of get, what is want is to return the list of books. So all we do now is we return, we retrieve the list of books, and we write the JSON of all those books. So default, what we do is, if it's not HTTP get for now, we will return an invalid status code.
We're done for this one. We'll build our executable again, and we will execute it. Let's see if this is already working. If I issue 8080/api/books, and I'm going to send this, you see here, there are the two books I've prepared in my program.
You see the correct content type headers, and the status code of 200. So let's add the code to create new books. Basically, what we will be using here, the post method. What we do is we call the ReadAll on the body, and we retrieve the byte array of the body.
What we need to do next is we will read the JSON from the body, and we will create the book. This is a basic business method I've prepared previously. All it does is create the book, put it into the dictionary, and return the ISBN, and return if the book has been created. If it has been created, it will return a status code of 201 and the location, and if it hasn't been created, it will return a status code conflict.
So this is what we do here. If it has been created, we add the location header, api/books, and we append the ISBN, and we return the header status created. And in the other case, we will return status conflict. So again, let's save that one. We go build, we call our microservice again, we use our REST client now, and you see here I call this URL with a post, and I have this body.
So basically, as a body I will pass the JSON structure of a new book. So I do send now, and here's the response. We have a status of 201 created, and the location for this for the following books. If I do a get request on all the books now, you see here, I have an additional book in my data store now.
This is the book we just created. Let's turn to the last handler function. What we want to do in here is again switch on the method, and depending on the method, we want to get the book, delete the book, or update the book. And this is all done via the ISBN which is in the path. So first of all, we will extract the ISBN as the last part from our path.
And then again, we're going to switch on the HTTP method. In case of a get, we will retrieve the book via ISBN. In case we've found the book, we will write back the book as JSON. And if we didn't find the book because the ISBN is unknown, we will return a status code of 404, which is status not found. So this is in case of get.
Let's add a new one, post. We will be using the put method to update our book. We will read the structure from the body, we unmarshal the body to a book structure, we will update the book, and in case the book existed we will say status OK, and in the other case we will say not found. Last but not least, we will be adding the delete method, which is pretty straightforward.
Again, we will call the DeleteBook function with the ISBN, and we say WriteHeader status okay. And in the other case, we will issue status bad request, because we do not support the request method. This should be it, let's try this. We build our microservice, and we execute it again. We switch to our REST client.
And remember, first of all, those are the books we're currently in. So let's get a book by ISBN. This is the get request, we pass the ISBN as a path parameter, and we're going to send it and we'll get one book back. This is expected. If we issue a get request for an unknown book using this ISBN, we do not get anything back, and we get the expected status code 404.
So this is okay. Let's create a new book in this case. We created a book, created this one, and let's update this book using a put request. So we update this book, Updated Book, going to send it, we have a status code of 200, and if we call all of our books again, we see that we updated the book again.
Last but not least, we want to try if the delete book is working. So again here, I've prepared this URL with the ISBN, I'll be using the HTTP delete method, and if I issue this one, everything's okay, and if I get the list of all books again, we're back to two books. Everything working as expected. And as you see, only in a few lines of Go code.
So in summary, what did we do? In the first part, we implemented a simple HTTP server using the net/http package of Go. In the second part, we then used the JSON package to marshal and unmarshal Go structures to and from JSON. And in this final part, we put everything together and implemented a simple REST API using a minimum of Go code. I hope you enjoyed this section, and I hope to see you in the next one, when we talk about the implementation of a Docker container for our microservice.
So see you then, when it's all about introduction to Docker and simple Go microservices and containerization. Bye bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Implementing Go HTTP Server
- JSON marshalling and unmarshalling of Go structs
- Implementing a simple REST API
- Using Docker workflows and commands
- Building a naïve Docker image
- Running a containerized Go microservice
- Kubernetes architecture and concepts
- Deploying a Go microservice to Kubernetes
- Implementing service descriptors
- Performing rolling updates