gohugoio/hugo · error

cannot parse as a media type

Error message

cannot parse %q as a media type

What it means

Returned by media.FromString when the input string does not split into exactly two parts on '/'. FromString expects 'MainType/SubType' (optionally 'MainType/SubType+suffix'); anything without exactly one slash, or with more than one, is rejected. This is the root cause often wrapped by 449.

Solutions

  1. Provide a value of the form 'MainType/SubType', e.g. 'text/html'.
  2. Use the optional '+suffix' form only when needed: 'application/rss+xml'.
  3. Check for accidental empty strings or path-like values passed where a MIME is expected.

Example fix

// before
media.FromString("markdown")   // error: cannot parse "markdown"

// after
media.FromString("text/markdown")
Defensive patterns

Strategy: validation

Validate before calling

// Validate a MIME string the way FromString does, before calling it.
func validMime(t string) bool {
    t = strings.ToLower(t)
    parts := strings.Split(t, "/")
    return len(parts) == 2 && parts[0] != "" && !strings.Contains(parts[1], "/")
}

Type guard

func isValidMediaType(t string) bool {
    parts := strings.Split(strings.ToLower(t), "/")
    if len(parts) != 2 {
        return false
    }
    return parts[0] != "" && !strings.Contains(parts[1], "/")
}

Try / catch

if _, err := media.FromString(t); err != nil {
    // fall back to a known-good default or reject the config
    return fmt.Errorf("invalid media type %q: %w", t, err)
}

Prevention

When it happens

Trigger: Calling media.FromString(t) with a string like "markdown", "text/html/extra", or "" (empty). Indirectly via DecodeTypes when a [mediaTypes] key is malformed. Also called from FromStringAndExt and content type lookups.

Common situations: Typing a bare subtype as a media type key; copying an extension instead of the MIME; trailing slash ('text/'); multiple slashes from path concatenation; an empty string from a missing config value.

Related errors


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

Appendix: source

Thrown at media/mediaType.go:141

	if err != nil {
		return tp, err
	}
	for i, e := range ext {
		ext[i] = strings.TrimPrefix(e, ".")
	}
	tp.SuffixesCSV = strings.Join(ext, ",")
	tp.Delimiter = DefaultDelimiter
	tp.init()
	return tp, nil
}

// FromString creates a new Type given a type string on the form MainType/SubType and
// an optional suffix, e.g. "text/html" or "text/html+html".
func FromString(t string) (Type, error) {
	t = strings.ToLower(t)
	parts := strings.Split(t, "/")
	if len(parts) != 2 {
		return Type{}, fmt.Errorf("cannot parse %q as a media type", t)
	}
	mainType := parts[0]
	subParts := strings.Split(parts[1], "+")

	subType := strings.Split(subParts[0], ";")[0]

	var suffix string

	if len(subParts) > 1 {
		suffix = subParts[1]
	}

	var typ string
	if suffix != "" {
		typ = mainType + "/" + subType + "+" + suffix
	} else {
		typ = mainType + "/" + subType
	}

View on GitHub (pinned to 52c9bd7908)