API returns malformed JSON but works in browser

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)


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? :thinking:

(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.)

Try to add a header req.Header.Add("Accept"," application/json")

Without api or server-side logic, it is difficult to see what is wrong.

1 Like

Thanks Eudore! I was indeed missing the Accept header. Adding that small line of code made the difference between an app that didn’t work and one that’s almost finished. Thanks again for your help. :slightly_smiling_face:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.