Attempting to Learn Go - Consuming a REST API

I’ve been thinking a lot about REST APIs recently. Working for a SaaS company seems to all but guarantee that you will come in contact with one (or dozens) so I suppose that makes sense. Since I’ve been working on some basic code and practicing my Go, I thought that it might be nice to jot down some notes and maybe some code to continue my Learning Go “series”.

Getting started with basic API consumption is pretty easy thanks to Go’s built in net/http package. Practically everything needed to query a remote API is available in the Go standard library.

We’ll start with our standard Go opening… and sticking with main for our package name since this is just an example. We’ll import fmt, io/ioutil, net/http - strictly speaking we don’t need fmt but since we’re not really doing anything with the API response, other then printing it out it’s included.

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

Next lets open our main function and declare our API URL, for this exercise we’re just going to query httpbin.org. Passing our URL and the “GET” method to http.NewRequest will give us back a proper http.Request object (if no error occurs) that we can use in the next step.

func main() {
    APIURL := "https://httpbin.org/get"
    req, err := http.NewRequest(http.MethodGet, APIURL, nil)
    if err != nil {
        panic(err)
    }

Using http we create a default client, this will handle the actual request to the remote server. Finally we’ll call Do() and actually make the request, receiving back a response or an error. It’s Go so we’ll check for errors and move on if we don’t have one.

    client := http.DefaultClient
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }

Assuming we received a valid response we’re going to begin to process it. First we’ll defer closing the response body, this way we can read out the data and Go will simply take care of closing it once the surrounding function returns. But first we’ll use ioutil.ReadAll to read in the body and return an array of bytes - as long as there is no error that is.

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

Finally, lets convert our array of bytes to a string and print it to standard out.

    fmt.Printf("%v", string(body))

And presto! That’s all there is to getting data from a remote REST endpoint in Go.

{
  "args": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "origin": "68.211.xx.xx",
  "url": "https://httpbin.org/get"
}

Next time I think I’ll expand this into a module that we can use for future REST work. From there we’ll probably write something that actually makes use of the consumed API.

Full code listing:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    APIURL := "https://httpbin.org/get"
    req, err := http.NewRequest(http.MethodGet, APIURL, nil)
    if err != nil {
        panic(err)
    }
  
    client := http.DefaultClient
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%v", string(body))
}