如何在Go中设置空的授权头(null authorization header)?

huangapple go评论87阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年11月27日 05:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/40823188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定