英文:
Infinite redirect loop with Gorilla toolkit
问题
我有这段简单的代码:
import (
"log"
"github.com/gorilla/http"
"bytes"
)
func main() {
url := "https://www.telegram.org"
log.Println("url: " + url)
var b bytes.Buffer
http.Get(&b, url)
log.Println("Get done")
}
它在进行GET请求的那一行卡住了。看起来它进入了一个无限循环的302响应,该响应将重定向到相同的URL("https://www.telegram.org")。
我是不是做错了什么或者做出了错误的假设?
谢谢和问候。
英文:
I have this simple code:
import (
"log"
"github.com/gorilla/http"
"bytes"
)
func main() {
url := "https://www.telegram.org"
log.Println("url: " + url)
var b bytes.Buffer
http.Get(&b, url)
log.Println("Get done")
}
and it freezes on the line making the GET request. It seems that it enters an infinite loop of 302 responses which redirects to the same url ("https://www.telegram.org").
Am I doing or assuming something wrong?
Thanks and regards.
答案1
得分: 2
显然,该库不支持https(哈哈)
https://github.com/gorilla/http/issues/8
所以只需使用stdlib的http模块:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
res, err := http.Get("https://www.telegram.org")
if err != nil {
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
fmt.Printf("%s", body)
}
英文:
Apparently that library doesn't support https (lol)
https://github.com/gorilla/http/issues/8
So just use the stdlib http module:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
res, err := http.Get("https://www.telegram.org")
if err != nil {
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
fmt.Printf("%s", body)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论