[Go] API client

Background
web client implementation is within the package net/http, this code uses this package to write an API client. Examples can be found in Golang. This API test website is used for testing the code. The endpoint which the code uses is /posts/1 as the json response is the shortest.

Postman
postman1
This is the response from postman, since it is a GET request using a browser is the same.
postman2
This is from the Mozilla Firefox browser, which prettify the json response.

Using Go to get API response
Two main packages are used to process the response, the first is net/http which uses the GET method to get the API response, the response then passed to the variable response, if there is an error a non-nil will be updated to err variable.
After the response is collected from the API endpoint, the response is sent to ioutil.ReadAll function to process and store to contents. The contents is a type uint8 which according to Golang documentation is the equivalent to byte.
In order to print out the contents in human readable “text”, contents is type casted with string type.

package main // Indicate to compile into binary

import (
	"fmt" // standard formatting
	"io/ioutil" // for reading response from api
	"net/http" // for using the Get method.
	"reflect" // for finding out the type of an object
)

// The main code starts from func main().
func main() {
	// returns error if url is unreachable, returns a response after api is called.
	response, err := http.Get("https://jsonplaceholder.typicode.com/posts/1")
	// error checking, error is nil if Get is successful, else Get is unsuccessful.
	if err != nil {
		fmt.Println(err) // displays the error.
		// Exit the program if there is connection error.
		return
	}
	// Close the request, it seems all resource termination has to use defer.
	// defer waits until all functions finished before execution.
	defer response.Body.Close()
	// The response body has to be parsed by the io stream
	// If there is no problem reading the stream, then error is nil,
	// else print error and exit program.
	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	// This is to check the contents object type
	// contents read from io stream is a byte,
	// according to golang documentation byte = uint8.
	// and contents is uint8 type.
	fmt.Println(reflect.TypeOf(contents))
	// This is required to "present" from byte to string.
	// string is a data type and it is doing type casting.
	// contents is type casted into a string so that human can read it.
	fmt.Println(string(contents))
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s