Golang http Get请求返回的response.Body为空。

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

Golang http Get empty response.Body

问题

我遇到了一个问题,对于某些URL,Golang代码无法检索到预期的内容。我没有发布实际的URL,但它具有以下形式,并且是指向Google Drive文件下载的链接:https://docs.google.com/uc?id=somelongid&export=download。如果我使用wget获取文件,它可以正常工作。我还有使用open()的Ruby代码,也可以正常工作。但是出于某种原因,Golang返回一个空缓冲区而没有错误。如果我使用此代码获取一些“普通”的URL,例如静态网站,它按预期工作并返回非空的response.Body。下面是我从项目中提取出来的代码,以简化和缩小问题可能出现的范围。以下是输出。

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	resp, err := http.Get("myurlishere")
	if err != nil {
		fmt.Println("Something went wrong")
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("Body: %v\n", body)
	fmt.Printf("Error: %v\n", err)
}

输出:

$go run main.go
Body: []
Error: <nil>
英文:

I am running into a problem that for certain urls, code in Golang is not retrieving the expected content. I am not posting the actual url, but it has this form and is a link to a google drive file download https://docs.google.com/uc?id=somelongid&amp;export=download. If I use wget to get the file it works fine. I also have ruby code which uses open() and it works as well. For some reason though Golang returns an empty buffer and no error. If I use this code to fetch some "ordinary" url like a static website it works as expected and returns non-empty response.Body. Below is code I extracted out of my project to simplify and narrow where the problem could be. Below is output.

package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;net/http&quot;
)

func main() {
	resp, err := http.Get(&quot;myurlishere&quot;)
	if err != nil {
		fmt.Println(&quot;Something went wrong&quot;)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf(&quot;Body: %v\n&quot;, body)
	fmt.Printf(&quot;Error: %v\n&quot;, err)
}

Output

$go run main.go
Body: []
Error: &lt;nil&gt;

答案1

得分: 2

也许你遇到了HTTP响应错误。

http.Get 不会返回错误,例如,如果服务器返回 404 - Not Found400 - Bad Request。这是因为它们不是异常行为,而是服务器的有效响应。

你应该检查 resp.StatusCode,确保它在 200 范围内。

你可以在包文档中查看不同的状态码。

这个问题表明使用Go下载Google Drive上的文件可能会遇到一些问题,因为它们在URL中使用了通配符,也许你可以尝试使用答案中提供的URL:https://googledrive.com/host/ID

英文:

Maybe you are getting an HTTP Response error.

http.Get will not return an error if, for example, the server answers with a 404 - Not Found, or 400 - Bad Request. It is because they are not exceptional behavior, they are valid responses from the server.

You should check resp.StatusCode and see that it is one in the 200 range.

You can check the different status codes in the package documentation.

This question suggests that there are some problems downloading from google drive with go, because they use wildcards in the url, maybe you can try using the url listed in the answer: https://googledrive.com/host/ID

huangapple
  • 本文由 发表于 2014年7月31日 10:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25050087.html
匿名

发表评论

匿名网友

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

确定