Last active
September 12, 2020 17:44
-
-
Save montanaflynn/b390b1212dada5864d9b to your computer and use it in GitHub Desktop.
GET JSON array in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package easyget | |
import ( | |
"io" | |
"io/ioutil" | |
"net/http" | |
) | |
// Define Request struct | |
type Request struct { | |
BaseUrl string | |
Endpoint string | |
QueryString string | |
Headers map[string][]string | |
} | |
// Define Response struct | |
type Response struct { | |
RawBody []byte | |
Body Body | |
Headers map[string][]string | |
Status int | |
Protocol string | |
} | |
// Define Body struct that goes in Response | |
type Body struct { | |
Bytes []byte | |
String string | |
} | |
func Get(req Request) (Response, error) { | |
// Build the URL | |
var url string | |
url += req.BaseUrl | |
url += req.Endpoint | |
url += req.QueryString | |
// Create an HTTP client | |
c := &http.Client{} | |
// Create an HTTP request | |
r, err := http.NewRequest("GET", url, nil) | |
if err != nil { return Response{}, err } | |
// Add any defined headers | |
if req.Headers != nil { | |
r.Header = http.Header(req.Headers) | |
} | |
// Add User-Agent if none is given | |
if r.Header["User-Agent"] == nil { | |
r.Header.Set("User-Agent", "Golang easyget") | |
} | |
// Send the request | |
res, err := c.Do(r) | |
// Check for error | |
if err != nil { return Response{}, err } | |
// Make sure to close after reading | |
defer res.Body.Close() | |
// Limit response body to 1mb | |
lr := &io.LimitedReader{res.Body, 1000000} | |
// Read all the response body | |
rb, err := ioutil.ReadAll(lr) | |
// Check for error | |
if err != nil { return Response{}, err } | |
// Build the output | |
responseOutput := Response{ | |
Body: Body { | |
Bytes: rb, | |
String: string(rb), | |
}, | |
Headers: res.Header, | |
Status: res.StatusCode, | |
Protocol: res.Proto, | |
} | |
// Send it along | |
return responseOutput, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"unirest" | |
"encoding/json" | |
) | |
// Define response data | |
type Data struct { | |
Date string | |
Cases int | |
Status string | |
} | |
func main() { | |
// Create any headers we want to send | |
headers := make(map[string][]string) | |
headers["X-Header-Info"] = []string{"Something"} | |
headers["X-Header-Array"] = []string{"One", "Two"} | |
// Build the Request struct | |
req := unirest.Request{ | |
BaseUrl: "http://192.241.227.237/", | |
Endpoint: "ebola/project", | |
QueryString: "?distance=5", | |
Headers: headers, | |
} | |
// Send the request | |
res, err := unirest.Get(req) | |
// Print error if it exists | |
if err != nil { fmt.Println(err) } | |
// Print the stuff in Response struct | |
fmt.Println(res.Body.String) | |
fmt.Println(res.Headers) | |
fmt.Println(res.Status) | |
fmt.Println(res.Protocol) | |
// Since we have a JSON array lets turn it into a Go array | |
var data []Data | |
json.Unmarshal(res.Body.Bytes, &data) | |
// Print what we got with keys | |
fmt.Printf("%+v\n", data) | |
// Loop over array and print some stuff we found | |
for _, e := range data { | |
fmt.Printf("%v total %v cases: %v \n", e.Date, e.Status, e.Cases) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try simplejson