英文:
How to set null authorization header in Go?
问题
我正在为从Github API获取数据的程序编写一个测试套件。我需要设置一个空的身份验证头。在使用curl
时,它可以正常工作,但在我的Go程序中却无法正常工作。
我尝试将其设置为建议的"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))
}
英文:
I am writing a test suite for my program that fetches data from Github APIs. I need to set an empty authentication header. It works fine with curl
but doesn't work in my Go program.
I tried setting it to "null" as suggested here. I've also tried nil
, ""
which don't work.
The output (too long to copy here but you can try it yourself) is as expected with curl -H "Authorization: " "https://api.github.com/repos/octocat/Hello-World/issues?state=open&per_page=1&page=1"
But here is the output with the same empty header set in Go:
{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
Here is the code (please don't suggest that I just remove the req.Header.Set()
line. I need to keep it in for my test suite)
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))
}
答案1
得分: 2
你的curl
命令没有设置空的Authorization
头;该curl
命令将不包括Authorization
头。你可以通过添加-v
参数来验证:
curl -H "Authorization: " -v "https://api.github.com/repos/octocat/Hello-World/issues?state=open&per_page=1&page=1"
话虽如此,在你的Go代码中也不应该设置Authorization
头。所以只需删除那一行,你的代码就可以立即正常工作。
英文:
Your curl
command does not set an empty Authorization
header; that curl
command will exclude the Authorization
header. You can verify it by adding the -v
parameter:
curl -H "Authorization: " -v "https://api.github.com/repos/octocat/Hello-World/issues?state=open&per_page=1&page=1"
That being said, you shouldn't set Authorization
header in your Go code either. So just remove that line, and your code becomes working immediately.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论