2
I’m trying to make Golang my first mid-high level language, but I’m having some problems. I can’t set a header for an external GET request at all, I searched the documentation, forums and the only thing I could find was to set a response header (for API creation). In my case, I need to set a header for a GET request for a certain API.
In the documentation of this API, it says that authentication is done through the header
Authentication: "TOKEN"
I tried several ways. Currently my code is like this, some good translator or language expert to explain to me how this header?
func get() {
API := "www.google.com"
TOKEN := "abcdefg"
resp, err := http.Get(API)
resp.Header.Set("Authentication", TOKEN)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
fmt.Printf("%+v\n", bodyString)
}
func main() {
get()
}
I understood that GET is not "parametravel", but even editing the code is still giving authentication error. I’m sure it’s in relation to the Request Headers because in JS is working and receiving the data from the API correctly, with the same token, same API, etcetera.
– Dasx
Could you post the API doc link? And the error message too.
– suriyel
{"message":"Authentication credentials have not been provided or are invalid. This error message is from the API, as I tested it in JS/php/Curl using the right/wrong token. It is the 403 return of the api.
– Dasx
@Dashadelas try to set the header this way:
req.Header.Set("Authorization", "Token " + TOKEN)
.– suriyel