REST API using GoLang

#Golang #REST

Today, every system has adapted the REST API. We have slowly moved from the SOAP to REST because it's easy to use and adaptability. I will explain the conceptual difference between REST and SOAP in a different post. For now, we will concentrate on the REST API. REST stands for Representational State Transfer. In this post, we are going to write a REST API using Golang. Why Golang? I don't know, I just like it. Okay, enough talking let's dive into the code. we will see an example of an e-commerce website.

Yay! Code

Our e-commerce website has a lot of products. Let's create a skeleton for our API implementation.

People who are new to Golang, this is the Hello World program of Golang. Now let's create a struct for holding our products.

We have three fields in the struct. The id, name, and price of the product. We have created it as a type called product and initialized a variable as an array of product. Now, all we have to do is to create a request handler to fill the data in the array. The JSON tags are used to map the appropriate struct fields to the JSON input data.

Here are the new packages that are to be imported. Mux package is very much easy to use for handling requests. we can also implement the same thing just using net/http package. But mux comes handy. ioutil is used to convert the request data into a slice. json is used to marshall the data into json format and vice-versa.

Now we initiate the router variable. And we create a handler function for each of the API endpoints. The createproduct is a post method, we can create a new item through this endpoint. The getproducts will get all the products that have been created from the start. The getproduct with parameter id will get the particular requested product. Finally, we are creating a server and made it listen to port localhost:8080. Now build it and run it, your server should be up and running.

Now, Let's complete the all the function definition. First, createproduct function.

So here we get the request and convert it into a slice and we store it into a separate variable. And then we unmarshal the slice and assign it into a new variable of type product. Then we append the newly created product to the slice of products. And finally, we send the newly created data as a response to the web client.

Now, let's test our code. We need a web client to test the handler functions individually. In this case, let's use Postman.

Here, you can see once we set the method as POST and enter the data into the body. Click send. That's it you can see the response in the response section back.

Now let's write the definition getproducts for displaying all the products that are created so far.

So, this function sends the products slice as the response to the client. let's see the postman response.

I have created some more data for visual purposes. Once you set the method as GET and send the request it displays all the products that are stored in the slice.

Now let's write the definition for getproduct. The getproduct function gets the product id from the request and it returns the product of that particular id.

So, we take id as a parameter from mux and store it in a variable. We iterate through all the products in the slice to get a match. Once matched we send it as a response.

Set the method as GET and give the id in the URL. In our case, I gave 2 as id. we can see the response sent from our program.

Github source code for this post.

Thanks for reading the post

Please share and subscribe to devdecimal.

Please give your valuable feedback in the comments.

if you haven't read the previous post, please proceed here.


©2020 by DevDecimal.