I am new to golang. I was writing a program to parse the json response of the API: https://httpbin.org/get. I have used the following code to parse the response:
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
type Headers struct {
Close string `json:"Connection"`
Accept string `json:"Accept"`
}
type apiResponse struct {
Header Headers `json:"headers"`
URL string `json:"url"`
}
func main() {
apiRoot := "https://httpbin.org/get"
req, err := http.NewRequest("GET", apiRoot, nil)
if err != nil {
fmt.Println("Couldn't prepare request")
os.Exit(1)
}
response, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
var responseStruct apiResponse
err = json.NewDecoder(response.Body).Decode(&responseStruct)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%v
", responseStruct)
}
When I run this code the output is:
$ go run parse.go
{{close } https://httpbin.org/get}
From the output, as we can see the "Accept" key in json response is not decoded. Why is it so? How can I parse that string from response body?