Skip to content

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.

Notifications You must be signed in to change notification settings

zetacoder/Music_Library_API_REST_GIN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Music Library API REST with GIN Framework

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.

STEP BY STEP GENERAL USE

STARTING MUSIC LIBRARY

A 10 music album library list allocated in our code. music_album_list

ROUTER, ROUTES AND HTTPS METHODS

Localhost:8080 used as server; Handlers made for GET, POST, PUT and DELETE HTTP requests; Handlers, routes and funcs made using GIN: router_routes_and_HTTP_methods

1) INITILIZE SERVER LISTENING

We run main.go in order to execute the code and run the local server: initialize_server initialize_server_code

We can check that the server is indeed running on the path: localhost:8080 in our browser: localhost_on_browser_1 localhost_on_browser_2

2) GET HTTP REQUEST

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: get_request_1

When the SEND button is clicked, the func 'getAlbums' is called by the handler of "/albums" route and is executed: get_request_3 get_request_4

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: get_request_2

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: get_request_6

When the SEND button is clicked, the func 'getAlbumsByID' is called by the handler of "/albums/id" route and is executed: get_request_8 get_request_7

We can see that we get as a response the music album requested by its ID (4), the data and a STATUS OK: get_request_5

3) POST HTTP REQUEST

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: post_request_1

When the request is sent, the handler for the route is executed and the function 'postAlbums' is called: post_request_2

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: post_request_4

If we try to Post an already existing ID (#11 from the previous example), it will response with a Status 400 code: post_request_5

The same response will occur if the Album name already exists: post_request_6

4) PUT HTTP REQUEST

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: put_request_1

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: put_request_3

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: put_request_4

5) DELETE HTTP REQUEST

Finally, we can use DELETE Method to erase a Music Album by ID. First, we use GET method to see all the list: delete_request_1

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 delete_request_2

We can see that the Album with ID 3 was erased succesfully with a 200 Status code: delete_request_3

If we try to DELETE a non-existing element, for example ID #100, it will throw a 404 Not Found Status: delete_request_4

CONCLUSION

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.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages