originally posted in:BungieNetPlatform
Hi there -
new to programming, but love Destiny, so thought as an intellectual exercise to learn Go, using Bungie APIs as a real world example.
However, i'm falling early - can someone pls look at my code, and tell me why I keep getting "Please add valid X-API-Key header to request" errors? I'm sure it's something stupid on my part :)
package main
import (
"net/http"
"net/http/httputil"
"io/ioutil"
"fmt"
"log"
)
func main() {
var body []byte
var response *http.Response
var request *http.Request
ApiToken := "PutYourAPIKeyHere"
user := "PutYourUsernameHere"
url := "https://www.bungie.net/Platform/Destiny/2/Stats/GetMembershipIdByDisplayName/" + user
request, err := http.NewRequest("GET", url, nil)
//if err == nil {
request.Header.Add("Content-Type", "application/json")
request.Header.Add("X-API-KEY", ApiToken)
debug(httputil.DumpRequestOut(request, true))
response, err = (&http.Client{}).Do(request)
//}
if err == nil {
defer response.Body.Close()
debug(httputil.DumpResponse(response, true))
body, err = ioutil.ReadAll(response.Body)
}
if err == nil {
fmt.Printf("%s", body)
} else {
log.Fatalf("ERROR: %s", err)
}
}
func debug(data []byte, err error) {
if err == nil {
fmt.Printf("%s\n\n", data)
} else {
log.Fatalf("%s\n\n", err)
}
}
-
Edited by VThornheart: 2/1/2016 8:19:06 PMAh, so the root problem is that the header name we're looking for is case sensitive. So instead of X-API-KEY it needs to be X-API-Key. Hopefully this helps! EDIT: It is not this, I obviously didn't get sufficient coffee this morning. :)
-
As one beginner trying to help another, when your code says: [quote]ApiToken := "PutYourAPIKeyHere"[/quote] Are you actually putting your API key here? If you haven't already registered to get an API key then please go here: [url]https://www.bungie.net/en/User/API[/url] to get one. Quick and painless. If, however, you are using good practices by not revealing your API key then I don't know what's wrong but maybe someone else can help. Cheers!