【发布时间】:2017-04-10 22:17:47
【问题描述】:
我正在为我的程序编写一个从 Github API 获取数据的测试套件。我需要设置一个空的身份验证标头。它适用于 curl,但不适用于我的 Go 程序。
我尝试按照here 的建议将其设置为“null”。我也尝试过nil、"",但都不起作用。
输出(此处复制太长但您可以自己尝试)与curl -H "Authorization: " "https://api.github.com/repos/octocat/Hello-World/issues?state=open&per_page=1&page=1" 的预期一致
但这是在 Go 中设置了相同的空标头的输出:
{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
这是代码(请不要建议我只删除req.Header.Set() 行。我需要将其保留在我的测试套件中)
func main() {
client := &http.Client{}
//issues API from Github
req, err := http.NewRequest("GET", "https://api.github.com/repos/octocat/Hello-World/issues?state=open&per_page=1&page=1", nil)
if err != nil {
log.Fatal(err)
}
//set authorization header. I have tried nil, and ""
req.Header.Set("Authorization", "null")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
//convert to a usable slice of bytes
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("couldn't read issues list", err)
}
fmt.Println(string(body))
}
【问题讨论】: