gohugoio/hugo · error
failed to create request for resource %s: %w
Error message
failed to create request for resource %s: %w
What it means
After decoding options, Hugo builds the `*http.Request` via `options.NewRequest(uri)`. This fails when `http.NewRequest` rejects the inputs — most commonly an invalid HTTP method string or a URL/body combination Go's http package won't accept. It fires before the request is sent, so it's a construction problem, not a network one.
Source
Thrown at resources/resource_factories/create/remote.go:252
if err := c.validateFromRemoteArgs(uri, options); err != nil {
return nil, err
}
getRes := func() (*http.Response, context.CancelFunc, error) {
ctx := context.Background()
var cancel context.CancelFunc
if perRequestTimeout > 0 {
ctx, cancel = context.WithTimeout(ctx, perRequestTimeout)
}
ctx = c.resourceIDDispatcher.Set(ctx, filecacheKey)
req, err := options.NewRequest(uri)
if err != nil {
if cancel != nil {
cancel()
}
return nil, nil, fmt.Errorf("failed to create request for resource %s: %w", uri, err)
}
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
if cancel != nil {
cancel()
}
return nil, nil, err
}
return resp, cancel, nil
}
res, cancel, err := getRes()
if cancel != nil {
defer cancel()
}View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Ensure the URL is absolute with an explicit scheme: `https://example.com/...`.
- Sanitize the `method` option: valid HTTP token, no whitespace, e.g. `dict "method" "post"`.
- Read the wrapped Go error text — it states exactly what http.NewRequest rejected — and correct that input.
Example fix
{{/* before */}}
{{ $r := resources.GetRemote "example.com/api" (dict "method" "POST ") }}
{{/* after */}}
{{ $r := resources.GetRemote "https://example.com/api" (dict "method" "post") }} Defensive patterns
Strategy: validation
Validate before calling
{{/* method must be a valid HTTP token; URL must be well-formed */}}
{{ $method := upper (default "get" .Params.method) }}
{{ if not (in (slice "GET" "POST" "HEAD") $method) }}
{{ errorf "unsupported method %q for %s" $method $url }}
{{ end }} Prevention
- Keep the method option a plain HTTP verb — embedded spaces or control characters make http.NewRequest fail
- Validate the URL (see index 323) — request construction fails on malformed URLs too
- Don't interpolate untrusted content directly into method or header names
When it happens
Trigger: Calling `resources.GetRemote` with a `method` option containing an invalid token (spaces, lowercase is normalized but e.g. `"GET "` with whitespace or a non-token character fails), or a URI form that parsed leniently in step one but is rejected by `http.NewRequest` (e.g. missing scheme in some forms), or a body option that can't form a request.
Common situations: Method values from data files with stray whitespace or quotes, using a relative or scheme-less URL (`//example.com/x` or `example.com/x`), or unusual custom methods with illegal characters.
Related errors
- failed to parse URL for resource %s: %w
- failed to decode options for resource %s: %w
- error building site: %w
- failed to detect format from content
- failed to detect target data serialization format
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/3831d956a2d56438.json.
Report an issue: GitHub ↗.