Using go to get api response in JSON

I cannot figure out how to pass into the get request an API Key, ConnectionID, or Authorization.

Most API’s do this and finding it troublesome to locate any syntax examples of doing this.

response, err := http.Get("myapiurl",

        url.Values{"connectionId": {"value"}, "apiKey": {"value"}, "authentication": {"value"}})

It would be helpful to know which API you’re using, as different ones expect you to authenticate in a different way.

Some may expect a client ID/secret in the query string, some may expect a specific header… but without knowing which, it’s difficult to help. :slight_smile:

If you look in the Go docs, they tell you that if you need to add header information, you can’t use http.Get.

Instead, you should use http.NewRequest and Client.Do:

client := &http.Client{
	CheckRedirect: redirectPolicyFunc,
}

resp, err := client.Get("http://example.com")
// ...

req, err := http.NewRequest("GET", "http://example.com", nil)
// ...
req.Header.Add("If-None-Match", `W/"wyzzy"`)
resp, err := client.Do(req)
// ...

In that example, they use Header.Add, but there are a few methods that deal with Headers — Add, Del, Get, Set, etc:
https://pkg.go.dev/net/http#Header

1 Like

Sending JSON is a bit more complicated than doing it in JavaScript or NodeJS.

This should help a bit.

But, I definitely recommend doing any requests involving API keys via POST. Usually on the server side it isn’t that big of a deal, but POST also doesn’t have a character size limit, while GET has a ~2k character limit.