经过身份验证的Golang HTTP客户端请求

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

authenticated http client requests from golang

问题

我有以下代码:

client := &http.Client{}

/* 验证身份 */
req, err := http.NewRequest("GET", "http://164.99.113.32/Authenticate", nil)
req.SetBasicAuth("<username>","<password>")
resp, err := client.Do(req)
if err != nil {
    fmt.Printf("错误 : %s", err)
}

/* 获取详细信息 */
req.URL, _ = url.Parse("http://164.99.113.32/Details")
resp, err = client.Do(req)
if err != nil {
    fmt.Printf("错误 : %s", err)
}

现在,第二个http调用失败,返回一个401访问被拒绝的错误。一个不同的REST客户端(一个Firefox插件)可以正确地从服务器获取详细信息,所以我知道服务器端没有问题。我需要传递一些在前一个请求中获得的会话字符串或其他东西吗?

英文:

I have the following code:

client := &amp;http.Client{}

/* Authenticate */
req, err := http.NewRequest(&quot;GET&quot;, &quot;http://164.99.113.32/Authenticate&quot;, nil)
req.SetBasicAuth(&quot;&lt;username&gt;&quot;,&quot;&lt;password&gt;&quot;)
resp, err := client.Do(req)
if err != nil {
    fmt.Printf(&quot;Error : %s&quot;, err)
}

/* Get Details */
req.URL, _ = url.Parse(&quot;http://164.99.113.32/Details&quot;)
resp, err = client.Do(req)
if err != nil {
    fmt.Printf(&quot;Error : %s&quot;, err)
}

Now, the second http call is failing with a 401 access-denied error. A different REST client (a firefox plugin) correctly gets the details from the server, so I know that nothing is wrong on the server side. Do I need to pass some kind of session string or something that we got in the previous request ?

答案1

得分: 25

好的。我已经解决了这个问题。我只需要创建一个cookie jar。

我很惊讶golang的http req/client类默认没有处理这个。

我需要使用的代码是:

type myjar struct {
    jar map[string] []*http.Cookie
}

func (p* myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
    fmt.Printf("URL是:%s\n", u.String())
    fmt.Printf("设置的cookie是:%s\n", cookies)
    p.jar[u.Host] = cookies
}

func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
    fmt.Printf("URL是:%s\n", u.String())
    fmt.Printf("返回的cookie是:%s\n", p.jar[u.Host])
    return p.jar[u.Host]
}

然后在main函数中:

    jar := &myjar{}
    jar.jar = make(map[string] []*http.Cookie)
    client.Jar = jar

可以工作。

英文:

Okay. I have resolved this. I just needed to create a cookie jar.

I am surprised that this is not handled by default by the golang http
req/client class.

The code that I had to use was:

type myjar struct {
    jar map[string] []*http.Cookie
}

func (p* myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
    fmt.Printf(&quot;The URL is : %s\n&quot;, u.String())
    fmt.Printf(&quot;The cookie being set is : %s\n&quot;, cookies)
    p.jar [u.Host] = cookies
}

func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
    fmt.Printf(&quot;The URL is : %s\n&quot;, u.String())
    fmt.Printf(&quot;Cookie being returned is : %s\n&quot;, p.jar[u.Host])
    return p.jar[u.Host]
}

and then in main:

    jar := &amp;myjar{}
    jar.jar = make(map[string] []*http.Cookie)
    client.Jar = jar

Works.

答案2

得分: 14

使用HTTP基本身份验证,凭据在每个请求中都是必需的。尝试在第二个client.Do(req)之前复制以下代码:

req.SetBasicAuth("<username>", "<password>")

Firefox插件能够获取详细信息的原因是浏览器会缓存HTTP基本身份验证令牌以供后续使用。

英文:

With HTTP Basic Authentication, the credentials are required for every request. Try copying the

req.SetBasicAuth(&quot;&lt;username&gt;&quot;, &quot;&lt;password&gt;&quot;)

line before the second client.Do(req).

The reason the Firefox plugin gets the details is that the browser will cache HTTP Basic
Authentication tokens for subsequent use.

答案3

得分: 4

这里有一个新的官方库,也许会有帮助。

http://golang.org/pkg/net/http/cookiejar/

示例:
https://stackoverflow.com/questions/18414212/golang-how-to-follow-location-with-cookie

英文:

There is a new official library, maybe this is helpful.

http://golang.org/pkg/net/http/cookiejar/

example:
https://stackoverflow.com/questions/18414212/golang-how-to-follow-location-with-cookie

huangapple
  • 本文由 发表于 2012年7月6日 19:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/11361431.html
匿名

发表评论

匿名网友

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

确定