I’ve worked with parsing JSON data from an API before, but now I get weird data in Go while making the same API request from the browser works.
This is the code I use:
req, err := http.NewRequest("GET", "example.com", nil)
if err != nil {
log.Fatalln("Failed to prepare API request")
}
client := &http.Client{Timeout: time.Second * 5}
// Make API request
request, err := client.Do(req)
if err != nil {
log.Fatalln("Error requesting API")
}
defer request.Body.Close()
// Read API response
buf := new(bytes.Buffer)
buf.ReadFrom(request.Body)
log.Println(buf.String())
The output has a non-JSON style I haven’t seen before:
({:previous_successful_build nil, :vcs-type :bitbucket, :canceler nil, :committer_name nil, :circle-yml nil, :infrastructure_fail false, :job-name nil, ...
If I go in the browser to the API url, I see the proper JSON format there:
What’s going wrong with my HTTP request in Go?
(I tried setting the Application-Type
header on the HTTP request but it makes no difference. I of course also looked at tutorials online and I cannot see a mistake in my code.)