gohugoio/hugo · error

failed to create request for resource

Error message

failed to create request for resource %s: %w

What it means

Wrapped error from `Client.FromRemote` (remote.go:247-253) when `options.NewRequest(uri)` fails. `NewRequest` (remote.go:431-446) calls `http.NewRequest` with the method, URL, and body reader; failure means the method is invalid, the URL is incompatible with `http.NewRequest` even though `url.Parse` accepted it, or the body reader setup errored.

Solutions

  1. Use a standard HTTP method: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, `OPTIONS` (uppercase).
  2. If a custom method is required, confirm Go's `http.NewRequest` accepts it (it permits arbitrary tokens, but rejects spaces/control chars).
  3. Trim/upper-case the method string before passing: `(upper (trim $method))`.

Example fix

// template -- before
{{ $r := resources.GetRemote $url (dict "method" " fetch ") }}

// after
{{ $r := resources.GetRemote $url (dict "method" "GET") }}
Defensive patterns

Strategy: validation

Validate before calling

import (
	"fmt"
	"strings"
)
var allowedMethods = map[string]bool{"GET":true,"POST":true,"PUT":true,"PATCH":true,"DELETE":true,"HEAD":true,"OPTIONS":true}
func validateMethod(m string) error {
	m = strings.ToUpper(strings.TrimSpace(m))
	if !allowedMethods[m] {
		return fmt.Errorf("unsupported method %q", m)
	}
	return nil
}

Prevention

When it happens

Trigger: Most commonly an invalid HTTP method string (e.g. `method = "FETCH"`, empty, or containing spaces). Also reachable if `http.NewRequest` rejects the URL for transport-level reasons (e.g. control characters in the path that `url.Parse` tolerated). Body reader errors are less common since `BodyReader()` is bytes-backed.

Common situations: Typo in method field; method coming from frontmatter with whitespace/newline; using a non-standard method the stdlib rejects; template interpolating an empty method string from a default.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/3831d956a2d56438. Report an issue: GitHub.

Appendix: 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 52c9bd7908)