当进行 HEAD 请求时,收到 EOF 错误响应。

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

Getting EOF error response when doing HEAD request

问题

我正在尝试对"https://speed.hetzner.de/100MB.bin"进行HEAD请求,以获取文件的元数据。但是,我收到了一个EOF错误响应。然而,对于相同的URL,GET请求是成功的。

可能的原因是什么?

我怀疑服务器已经阻止了HEAD请求,但是如何验证呢?

PS:添加代码:

func BuildRequest(method string, url string) (*http.Request, error) {
	r, err := http.NewRequest(method, url, nil)
	r.Header.Set("User-Agent", "my app")
	return r, err
}

func (m *FileMeta) Fetch(url string) error {
	r, err := BuildRequest(http.MethodHead, url)
	if err != nil {
		return err
	}

	resp, err := HTTPClient().Do(r)

	if err != nil {
		return err
	}

	defer resp.Body.Close()
}

错误截图:
当进行 HEAD 请求时,收到 EOF 错误响应。

英文:

I am trying to do a HEAD request at "https://speed.hetzner.de/100MB.bin" to get the metadata of the file. I am served with an EOF error response. However, the GET request is successful for the same URL.

What could be the possible reasons?

I suspect that the server has blocked HEAD requests, but how do I verify it?

PS: adding the code:

func BuildRequest(method string, url string) (*http.Request, error) {
	r, err := http.NewRequest(method, url, nil)
	r.Header.Set("User-Agent", "my app")
	return r, err
}

func (m *FileMeta) Fetch(url string) error {
	r, err := BuildRequest(http.MethodHead, url)
	if err != nil {
		return err
	}

	resp, err := HTTPClient().Do(r)

	if err != nil {
		return err
	}

	defer resp.Body.Close()
}

Error screenshot:
当进行 HEAD 请求时,收到 EOF 错误响应。

答案1

得分: 2

你还没有展示给我们任何代码...

...但听起来你正在尝试从一个HEAD请求中读取HTTP的"响应"(而不仅仅是访问HTTP响应头)。

根据RFC:

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

HEAD方法与GET方法相同,只是服务器在响应中不得返回消息主体。响应头中包含的元信息应与对GET请求的响应中发送的信息相同。该方法可用于获取有关请求所暗示的实体的元信息,而无需传输实体本身。该方法通常用于测试超文本链接的有效性、可访问性和最近修改。

换句话说,对HEAD请求的响应没有主体。如果你尝试读取一个主体,你将得到一个EOF(文件结束符)。我怀疑这就是这里发生的情况。


感谢你更新问题并提供了这段代码。看起来你正在使用Go语言。

尝试使用以下代码:

res, err := http.Head("https://golang.org")
if err != nil {
    panic(err)
}
println(res.StatusCode)

记住:res没有主体


补充说明

– Rajesh Sethi

我尝试使用Postman,它没有响应HEADER信息。
我尝试使用Curl,它显示"Empty reply from server"。

上面的代码resp, err := http.Head("speed.hetzner.de/100MB.bin"),抛出了相同的"EOF"错误。

– paulsm4

很好 - 谢谢。所以听起来你最初的推测是正确的:

  1. 你的代码没问题:你没有"做错什么"(比如尝试读取一个不存在的"Body")。

  2. 相反,那个特定的URL(speed.hetzner.de)正在阻止(或至少拒绝处理)HEAD请求。

英文:

You haven't shown us any code ...

...but sounds like you're trying to read an HTTP "response" from a HEAD request (instead of only accessing the HTTP response headers).

Per the RFC:

> https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
>
> The HEAD method is identical to GET except that the server MUST NOT
> return a message-body in the response. The metainformation contained
> in the HTTP headers in response to a HEAD request SHOULD be identical
> to the information sent in response to a GET request. This method can
> be used for obtaining metainformation about the entity implied by the
> request without transferring the entity-body itself. This method is
> often used for testing hypertext links for validity, accessibility,
> and recent modification.

In other words, the response to a HEAD request doesn't have a body. If you try to read one, you'll get an EOF. I suspect that's what's happening here.


Thank you for updating your question with this code. It looks like you're using Go.

Try this:

res, err := http.Head("https://golang.org")
if err != nil {
    panic(err)
}
println(res.StatusCode)

And remember: res will have no Body.


ADDENDUM

– Rajesh Sethi
> I did try with Postman, it didn't respond with HEADER information.
> I tried Curl, it says "Empty reply from server".
>
> The code above, resp, err := http.Head("speed.hetzner.de/100MB.bin"), throws the same "EOF" error.

– paulsm4

> Cool - thank you. So it sounds like your original surmise is correct:
> 1) Your code is OK: you're not "doing anything wrong" (like trying to read a "Body" that isn't there).
>
> 2) Rather, that particular URL
> (speed.hetzner.de) is blocking (or at least refusing to process) HEAD requests.

huangapple
  • 本文由 发表于 2021年8月9日 07:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/68705334.html
匿名

发表评论

匿名网友

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

确定