1

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: enter image description here

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Rajesh Sethi
  • 179
  • 2
  • 11
  • Q: did you confirm that you can get a valid HEAD response independent of Go (did you try curl, Postman or equivalent? Did they response with HEADER information?) Q: Did you find a viable "Go" code solution (e.g. `http.Head()`)? Please let us know! – paulsm4 Aug 10 '21 at 16:47
  • I did try with Postman, it didn't respond with HEADER information. I tried Curl, it says "Empty reply from server". – Rajesh Sethi Aug 10 '21 at 20:30
  • ``` resp, err := http.Head("https://speed.hetzner.de/100MB.bin") if err != nil { return err } ``` tried the above code as you suggested. it throws the same "EOF" error. – Rajesh Sethi Aug 10 '21 at 20:37
  • 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. – paulsm4 Aug 10 '21 at 21:20

1 Answers1

2

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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • it fails at HTTPClient().Do(r) and returns error – Rajesh Sethi Aug 09 '21 at 09:05
  • 1
    Sooooo .... you're trying to make the HEAD request from Go. SUGGESTIONS: 1) Try "HEAD" using a *different* method, e.g. [curl](https://curl.se/) or [Postman](https://www.postman.com/downloads/). Verify "it works". 2) Next, try this example, using `http.Head()`): https://stackoverflow.com/a/38563299/421195, 3) If you specify the method, be sure to use the Go const [HEAD](https://pkg.go.dev/net/http#pkg-constants) – paulsm4 Aug 09 '21 at 15:12