loading...
Cover image for Attempting to Learn Go - Continuing REST Adventures

Attempting to Learn Go - Continuing REST Adventures

shindakun profile image Steve Layton ・2 min read

Attempting to Learn Go (23 Part Series)

1) Attempting to Learn Go - Building a Downloader Part 01 2) Attempting to Learn Go - Building a Downloader Part 02 3 ... 21 3) Attempting to Learn Go - Building a Downloader Part 03 4) Attempting to Learn Go - Building a Downloader Part 04 5) Attempting to Learn Go - Building a Downloader Part 05 6) Attempting to Learn Go - Consuming a REST API 7) Attempting to Learn Go - Continuing REST Adventures 8) Attempting to Learn Go - Now Sending REST Requests 9) Attempting to Learn Go - REST API and A Bit On Templates 10) Attempting to Learn Go - Sending Email Via API Again 11) Attempting to Learn Go - Let's Get Modular! 12) Attempting to Learn Go - Let's Get Modular - Again! 13) Attempting to Learn Go - Building Dev Log Part 01 14) Attempting to Learn Go - Building Dev Log Part 02 15) Attempting to Learn Go - Building Dev Log Part 03 16) Attempting to Learn Go - Building Dev Log Part 04 17) Attempting to Learn Go - Building Dev Log Part 05 18) Attempting to Learn Go - Listing Files By Extension 19) Attempting to Learn Go - Sorting and Moving Files by Extension 20) Attempting to Learn Go - Issuer 01 21) Attempting to Learn Go - Issuer 02 22) Attempting to Learn Go - Issuer 03 - Cloud Function Go! 23) Attempting to Learn Go - Issuer 04 - Addendum

Continuing Adventures

Last time we put together a small program to query a REST API and print out the response. Not overly useful but a good learning experience none the less. This time we're going to take that and actually try to do something with it - hopefully, I'll figure out what before we get to the end of the post.


The JSON Response

In order to use the JSON response from httpbin.org and we'll need to get it from JSON into a structure Go can work with. Using our example response from the previous post we can take a shortcut to create our struct over at JSON-to-Go site

Here's our original object...

{
  "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"
}

And our auto-generated result...

type AutoGenerated struct {
  Args struct {
  } `json:"args"`
  Headers struct {
    AcceptEncoding string `json:"Accept-Encoding"`
    Connection     string `json:"Connection"`
    Host           string `json:"Host"`
    UserAgent      string `json:"User-Agent"`
  } `json:"headers"`
  Origin string `json:"origin"`
  URL    string `json:"url"`
}

That's a pretty good translation but, we don't want exactly what the site spat out at us. We're going to change the Args and the Header sections a bit making them a map.

type HTTPBinResponse struct {
  Args    map[string]string `json:"args"`
  Headers map[string]string `json:"headers"`
  Origin  string            `json:"origin"`
  URL     string            `json:"url"`
}

A map is basically a key/value pair, which is perfect for holding query parameters and headers. This makes our struct a bit simpler to use and we can just loop through Args, or Headers if need be.

We have only made a few other small changes from the last post so I won't go back through the entire thing again. First, we've updated the URL we are calling to include two query parameters. This is so we can use our new Args map.

  APIURL := "https://httpbin.org/get?arg1=one&arg2=two"

Jumping down to after our response is received we create a new variable r of type HTTPBinResponse. This holds the struct we created for our response. Next, we use json.Unmarshal to "un-marshal" the JSON object into our struct.

  var r HTTPBinResponse
  json.Unmarshal(body, &r)

Now all's that if left is - that's right printing the data. We'll print it out a couple different ways, first, the full struct and the User Agent returned and finally the second argument. In practice you may not know exactly which headers (other than the standard HTTP headers) or arguments are returned but since this is just an example we just go for it.

  fmt.Printf("%#v\n\n", r)
  fmt.Printf("%v\n\n", r.Headers["User-Agent"])
  fmt.Printf("%v\n", r.Args["arg2"])

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



Attempting to Learn Go (23 Part Series)

1) Attempting to Learn Go - Building a Downloader Part 01 2) Attempting to Learn Go - Building a Downloader Part 02 3 ... 21 3) Attempting to Learn Go - Building a Downloader Part 03 4) Attempting to Learn Go - Building a Downloader Part 04 5) Attempting to Learn Go - Building a Downloader Part 05 6) Attempting to Learn Go - Consuming a REST API 7) Attempting to Learn Go - Continuing REST Adventures 8) Attempting to Learn Go - Now Sending REST Requests 9) Attempting to Learn Go - REST API and A Bit On Templates 10) Attempting to Learn Go - Sending Email Via API Again 11) Attempting to Learn Go - Let's Get Modular! 12) Attempting to Learn Go - Let's Get Modular - Again! 13) Attempting to Learn Go - Building Dev Log Part 01 14) Attempting to Learn Go - Building Dev Log Part 02 15) Attempting to Learn Go - Building Dev Log Part 03 16) Attempting to Learn Go - Building Dev Log Part 04 17) Attempting to Learn Go - Building Dev Log Part 05 18) Attempting to Learn Go - Listing Files By Extension 19) Attempting to Learn Go - Sorting and Moving Files by Extension 20) Attempting to Learn Go - Issuer 01 21) Attempting to Learn Go - Issuer 02 22) Attempting to Learn Go - Issuer 03 - Cloud Function Go! 23) Attempting to Learn Go - Issuer 04 - Addendum

Posted on by:

shindakun profile

Steve Layton

@shindakun

I've been known to write some code from time to time.

Discussion

markdown guide