英文:
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 := &http.Client{}
/* Authenticate */
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("Error : %s", err)
}
/* Get Details */
req.URL, _ = url.Parse("http://164.99.113.32/Details")
resp, err = client.Do(req)
if err != nil {
fmt.Printf("Error : %s", 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("The URL is : %s\n", u.String())
fmt.Printf("The cookie being set is : %s\n", cookies)
p.jar [u.Host] = cookies
}
func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
fmt.Printf("The URL is : %s\n", u.String())
fmt.Printf("Cookie being returned is : %s\n", p.jar[u.Host])
return p.jar[u.Host]
}
and then in main:
jar := &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("<username>", "<password>")
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论