golang/go · error

could not parse URL %s: %v

Error message

could not parse URL %s: %v

What it means

Raised inside parseUserAuth for each non-blank URL line: the line must pass url.ParseRequestURI, which requires an absolute URI with a scheme. If parsing fails, this error names the offending line and wraps the parse error.

Source

Thrown at src/cmd/go/internal/auth/userauth.go:70

// See the expected format in 'go help goauth'.
func parseUserAuth(data string) (map[string]http.Header, error) {
	credentials := make(map[string]http.Header)
	for data != "" {
		var line string
		var ok bool
		var urls []string
		// Parse URLS first.
		for {
			line, data, ok = strings.Cut(data, "\n")
			if !ok {
				return nil, fmt.Errorf("invalid format: missing empty line after URLs")
			}
			if line == "" {
				break
			}
			u, err := url.ParseRequestURI(line)
			if err != nil {
				return nil, fmt.Errorf("could not parse URL %s: %v", line, err)
			}
			urls = append(urls, u.String())
		}
		// Parse Headers second.
		header := make(http.Header)
		for {
			line, data, ok = strings.Cut(data, "\n")
			if !ok {
				return nil, fmt.Errorf("invalid format: missing empty line after headers")
			}
			if line == "" {
				break
			}
			name, value, ok := strings.Cut(line, ": ")
			value = strings.TrimSpace(value)
			if !ok || !validHeaderFieldName(name) || !validHeaderFieldValue(value) {
				return nil, fmt.Errorf("invalid format: invalid header line")
			}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Always include the scheme and host: `https://example.com/path`.
  2. Trim trailing whitespace/CR from each URL line.
  3. Percent-encode any spaces or non-ASCII bytes in the path/query.
  4. Test lines with Go's `url.ParseRequestURI` before emitting them.

Example fix

// before
example.com
// after
https://example.com
Defensive patterns

Strategy: validation

Validate before calling

// Validate each URL line before emitting it.
if _, err := url.ParseRequestURI(line); err != nil {
    return fmt.Errorf("invalid URL line %q: %w", line, err)
}

Prevention

When it happens

Trigger: A URL line that is not an absolute request URI: a bare hostname (`example.com`), a path without scheme (`/foo`), a line containing spaces or control characters, or invalid percent-encoding.

Common situations: Writing `example.com` instead of `https://example.com`; trailing whitespace or a carriage return; non-ASCII characters; unencoded spaces in the path.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/97f835eeaa523f68. Report an issue: GitHub.