Attempting to Learn Go - Consuming a REST API
Tags: go, continuingeducation, atlg
Published: 2018-11-21 23:09:25

---
title: Attempting to Learn Go - Consuming a REST API
published: true
description: 
tags: go, golang, continuing-education, atlg
cover_image: https://shindakun.glitch.me/content/images/2018/01/attemptinggo.png
series: "Attempting to Learn Go"
---

I've been thinking a lot about REST APIs recently. Working for a SaaS company seems to all but guarantees 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 than printing it out it's included.

```go
package main

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

Next let's 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.

```go
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.

```go
    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.

```go
    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.

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

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

```json
{
  "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.

***

You can find the code for this and most of the other Attempting to Learn Go posts in the repo on GitHub.

{% github shindakun/atlg %}

***

| Enjoy this post? |
|:----:|
| [How about buying me a coffee?](https://ko-fi.com/shindakun) |

***