Introducing Restful APIs in GO

  • Reading JSON in GO.
  • Hitting an RESTful GET API in GO.
  • Hitting an RESTful POST API in GO.
  • Exposing and hosting Simple Restful API in GO.
  • Json is a good default since it’s widely used in many languages and services supported.
  • Json can also easily be passed from the browser making it the preferred encoding is Rest APIs.
  • All the fields must start with an uppercase.
  • Another crucial thing is to indicate the json’s field mapping with this structure. So, we have Login string and then we use a field tag to say to json that in json the name of this field is user.
  • Similarly, we have the Type and we say to the json this is a lower case type in the json.
  • And again the Amount which is the lowercase amount in the json.
  • Create a NewBufferedString from bytes. This create an object of reader that we can use with the json decoder.
  • Next, we create a json NewDecoder with a reader.
  • Then, we create an empty request and then ask the decoder to decode the request.
  • And if the decode resulted in an error we use log.Fatalf to print error and abort the program. Otherwise we print what we got.
  • 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).
  • Now, we create a NewEncoder for the standard output just to print it out and we encode the response. And of course if there is an error in the encoding, we’re going to use log.fatalf to print it out.
  • A common way to communicate between services is by using HTTP and JSON, also known as REST API.
  • HTTP plus JSON is very common, and chances are you’ll have to work with them at one point or another.
  • We’ll use the service, to test your HTTP client.
  • We do an http.Get to above mentioned URL.
  • At line #13, we get the response and an error. If there is an error, we use log.Fatalf to exit the program.
  • At line #17, make sure to close the response body. We use defer to make sure the response body is closed.
  • Finally, we’re just going to print out what the server sent us, using io.Copy from the response body to standard output.
  • At line #22, we are going to create a new Job object.
  • At line #28, we create bytes.buffer, we use a JSON encoder with the buffer, and we encode the job inside the in memory buffer, and if there is an error, we exit.
  • The http post function will cause the request body to be io.writer. We’re going to use bytes with buffer, which is an in memory writer or reader.
  • In the http.Post method, we tell it what is the url that we want to hit to, we tell it the content type, and it’s really important for a lot of APIs to send the right content type, and we send the in memory buffer which now will be an io.reader.
  • Now, if there is an error making the post request, we are going to fail, and again, defer the Body.Close, and copy the output to standard output.
  • 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.
  • A pointer to an http.Request, which represents the current request.



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
aditya goel

aditya goel

Software Engineer for Big Data distributed systems