Simple API REST to GET, POST, PUT and DELETE music albums using Go and GIN framework. To test the application I used POSTMAN in order to emule GET, POST, PUT and DELETE http methods.
A 10 music album library list allocated in our code.
Localhost:8080 used as server; Handlers made for GET, POST, PUT and DELETE HTTP requests; Handlers, routes and funcs made using GIN:
We run main.go in order to execute the code and run the local server:
We can check that the server is indeed running on the path: localhost:8080 in our browser:
We use POSTMAN to test our API in a simply manner. First, we select the GET method and set the path (localhost:8080) and SEND the request:
When the SEND button is clicked, the func 'getAlbums' is called by the handler of "/albums" route and is executed:
We get the response from the request and we can see that we get the whole list of our music albums and STATUS 200 OK as response:
We can also use a GET Request searching for an Album by ID, using the route: "/albums/ id_number ". For example, we can try to request the album #4:
When the SEND button is clicked, the func 'getAlbumsByID' is called by the handler of "/albums/id" route and is executed:
We can see that we get as a response the music album requested by its ID (4), the data and a STATUS OK:
Now, we´re going to POST a new Album in our library. To do that, first we prepare the METHOD (POST), ROUTE (localhost:8080/albums) and we must fill the Body section with the NEW MUSIC ALBUM. In most cases, its used JSON format:
When the request is sent, the handler for the route is executed and the function 'postAlbums' is called:
If everything went good, we can see that the New Music Album was created and added to the library with a 201 Created Status code:
If we try to Post an already existing ID (#11 from the previous example), it will response with a Status 400 code:
The same response will occur if the Album name already exists:
Now we´re going to replace an existing album with another using the ID of the Album to replace it. First, we make a GET request to know the existing Music Albums. We are going to replace ID: 2, "Hybrid Theory" by Linkin Park:
To replace that resource, we sent the new data (JSON format) in the body of the request with POST method using the same ID (2). We can see that as response we have a 200 OK STATUS and now in the ID #2 the older album was replaced:
Finally if we try to do a PUT request using a non-existing ID (p.e "99") it will throw a 400 Bad Request STATUS as shown below:
Finally, we can use DELETE Method to erase a Music Album by ID. First, we use GET method to see all the list:
Lets say we want to DELETE registry with ID 3 ("Ser Humano Album"). To do that we need to specify in the route which ID element we´re going to erase. In this case, it would be localhost:8080/albums/3
We can see that the Album with ID 3 was erased succesfully with a 200 Status code:
If we try to DELETE a non-existing element, for example ID #100, it will throw a 404 Not Found Status:
By way of conclusion we can say that using GIN framework to create a REST API application is extremely simple, useful, powerful and with few lines of code, providing the necessary functionality to deploy a server securely, handle responses to and from the client and all with the simple and elegant Golang syntax.