Introducing Restful APIs in GO

In case you are landing here directly, it’s strongly suggested that you go and read through this for fundamentals.

In this blog, we shall be looking at following concepts :-

  • Reading JSON in GO.

Question :- What are the possible ways of encoding the messages ?

Answer:- When you’re working in a distributed environment usually over the network you send and receive messages. Messages can be encoded in various formats such as : json, messages pack, particle buffer, XML and more.

Question :- Which format is more widely accepted ?

Answer:-

  • Json is a good default since it’s widely used in many languages and services supported.

Question :- What’s the take of GO with regards to handling JSON ?

Answer:- Go comes with a built in encoding/json library. We can either encode the code to IO reader, IO writer or work with byte slices.

Question :- Demonstrate any use-case of reading JSONs with GO ?

Answer:- Say you are running an online bank, you get request for withdraw & deposit money and you need to pass them and handle them.

Step #1.) Here is how our JSON request looks like :-

Step #2.) Let’s define the corresponding request structure which mimics the the json object.

  • All the fields must start with an uppercase.

Step #3.) In our main program we perform following works :-

  • Create a NewBufferedString from bytes. This create an object of reader that we can use with the json decoder.

Question :- Demonstrate any use-case of generating the JSONs with GO ?

Answer:- We would continue with our above use-case itself and generate an JSON based output :-

Step #1.) Here are dependencies we would be importing. Don’t forget to import the OS package :-

Step #2.) Next, here is how the code looks like :-

  • At line #42, we’re going to create a response where the keys are strings and the value is empty interface (In Go, it means any type).

Question :- How do services usually communicates ?

Answer:-

  • A common way to communicate between services is by using HTTP and JSON, also known as REST API.

Question :- Does there exists any inbuilt support for http calls in GO ?

Answer:- You’ll find most of what you need in the package net/http.

Question :- Let’s start making a GET request in GO ?

  • We’ll use the httpbin.org/get service, to test your HTTP client.

Question :- Let’s make a post request with the JSON body in GO.

Step #1.) :- We define a Job structure, the User, the Action, and how many times you want the action to be performed.

Step #2.) In our main function (as shown in below snapshot):-

  • At line #22, we are going to create a new Job object.

Question :- Can we launch RESTful APIs in GO ?

Answer:- Go comes with a powerful HTTP server in the standard library. It’s easy to build the REST APIs with it. We write handler functions to expose APIs.

Question :- Whats an Handler Function in GO ?

Answer:- A handler function is a function that gets following parameters :-

  • http.ResponseWriter where you write the response to. Since the http.ResponseWriter implements io.writer, we can use the Fprintf to print the output to it.

Question :- Demonstrate the process of exposing a Restful API in GO ?

Step #1.) Following are the libraries we would be importing :-

Step #2.) We need to mount the endpoint to the Handler. This means that every time someone accesses /hello on our web server, helloHandler is going to get called.

And now, we tell the HTTP server to listen and serve on port 8080. This is potentially an endless loop. But if there is an error, it will exit with an error value. And in our case, we’re going to print out the error and exit the program.

Step #3.) Here is how the code of handler function looks like :-

Step #4.) Let’s save it and run it. We built a production-ready HTTP server in 20 lines of code. I find it pretty impressive.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store