oclero
January 5, 2023, 4:20pm
1
Hello,
I would like to make a HEAD request, in order to get the size in bytes of a file to download.
I’ve added HEAD
in the allowed methods:
[security]
[security.http]
methods = ['(?i)GET|POST|HEAD']
Also, I’ve tried to specify the method in the GetRemote
options:
{{- $options := dict
"method" "HEAD"
-}}
{{- $url := "https://some-website.com/some-endpoint" -}}
{{- with resources.GetRemote $url $options -}}
{{- . -}}
{{- end -}}
Unfortunately, I get the same thing as if I did a GET request.
bep
January 5, 2023, 5:05pm
2
I’m pretty sure we pass on the method correctly to Go’s HTTP client. Are you sure the HEAD behaves differently on this particular URL?
When I do this:
{{ $url := "https://gohugo.io/img/hugo.png" }}
{{ $opts := dict "method" "head" }}
{{ with resources.GetRemote $url $opts }}
{{ with .Err }}
{{ errorf "Unable to get remote resource: %s" . }}
{{ else }}
{{ .Content }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource: %s" $url }}
{{ end }}
I get:
Unable to get remote resource: error calling resources.GetRemote: failed to read remote resource “https://gohugo.io/img/hugo.png ”: unexpected EOF
But the response is captured and cached.
/tmp/hugo_cache/my-project/filecache/getresource/6247740232174893946
HTTP/2.0 200 OK
Content-Length: 18210
Accept-Ranges: bytes
Age: 0
Cache-Control: public, max-age=0, must-revalidate
Content-Type: image/png
Date: Thu, 05 Jan 2023 18:18:44 GMT
Etag: "32518b4aa48eb4feb7513529c2fbdfde-ssl"
Link: </dist/app.bundle.js>; rel=preload; as=script, </dist/main.css>; rel=preload; as=style
Referrer-Policy: origin-when-cross-origin
Server: Netlify
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Nf-Request-Id: 01GP1JG8YJZ1VN1CX49TKB067J
X-Xss-Protection: 1; mode=block
oclero
January 5, 2023, 5:38pm
4
I don’t think so. I am trying with this URL: "https://gitlab.com/api/v4/projects/6933865/releases/v1.12.3"
When I use curl
, I get the head response correctly, but when I use with Hugo, and print the result with {{- . -}}
, I get the name of the JSON file it will download with a GET request.
oclero
January 5, 2023, 5:57pm
5
I got the same response when I tried to get the head data for a file (which is my final goal).
Are you trying to get the size of an asset tied to a GitHub release?
oclero
January 5, 2023, 6:34pm
9
Thank you! Github release API has size
in the GET response, so it works.
Gitlab release API doesn’t has such a thing. This is why I was tring to get it with another mean.
Understood. The HEAD
request method appears to be broken. It looks like we have some test coverage for GET
and POST
, but not for HEAD
. I will create a GH issue.
https://github.com/gohugoio/hugo/issues/10604
Can you provide an example GitLab API call to a repo that has assets? I know I’ve used the API before, but I don’t remember the context.
oclero
January 6, 2023, 10:31am
11
Another way would be to contribute to Gitlab API and add the size
information, but the Gitlab project is absolutely gigantic and I don’t even know where to find stuff.
Anyway, here is a repo we will actually use:
To get a specific release from the API:
This is the asset I would like to know the size, by first making a HEAD request. However, this is a redirect , and the HEAD gives me a location
URL which is the actual URL for downloading the asset.
The URL you should finally get is https://gitlab.com/solarus-games/games/zsdx/uploads/cf5a3455d139ef804e2cb6f84bfebe52/zsdx-v1.12.3.solarus . By making a HEAD request to this URL, you can finally get content-length
.
I don’t know if this is the best example to use to add a test because of the redirection.
Thank you for the links.
oclero:
contribute to Gitlab API
I ran a couple of searches against the 46k open issues and didn’t find anything. I was surprised. I think getting their attention on this would be difficult.
oclero
January 6, 2023, 5:16pm
13
Thanks for your help. Tell me if you need anything else.
I’ll write a Hugo partial that I can share here when the HEAD request works.
2 Likes