grpc-ecosystem/grpc-gateway · error

empty MIME type

Error message

empty MIME type

What it means

This error is returned by marshalerRegistry.add when registering a Marshaler with an empty MIME type string. The registry keys marshalers by a case-sensitive MIME type ("*" matches any), so an empty key would create an unusable, unmatchable entry. The library refuses the registration instead of silently storing it.

Source

Thrown at runtime/marshaler_registry.go:77

		inbound = mux.marshalers.mimeMap[MIMEWildcard]
	}
	if outbound == nil {
		outbound = inbound
	}

	return inbound, outbound
}

// marshalerRegistry is a mapping from MIME types to Marshalers.
type marshalerRegistry struct {
	mimeMap map[string]Marshaler
}

// add adds a marshaler for a case-sensitive MIME type string ("*" to match any
// MIME type).
func (m marshalerRegistry) add(mime string, marshaler Marshaler) error {
	if len(mime) == 0 {
		return errors.New("empty MIME type")
	}

	m.mimeMap[mime] = marshaler

	return nil
}

// makeMarshalerMIMERegistry returns a new registry of marshalers.
// It allows for a mapping of case-sensitive Content-Type MIME type string to runtime.Marshaler interfaces.
//
// For example, you could allow the client to specify the use of the runtime.JSONPb marshaler
// with an "application/jsonpb" Content-Type and the use of the runtime.JSONBuiltin marshaler
// with an "application/json" Content-Type.
// "*" can be used to match any Content-Type.
// This can be attached to a ServerMux with the marshaler option.
func makeMarshalerMIMERegistry() marshalerRegistry {
	return marshalerRegistry{
		mimeMap: map[string]Marshaler{

View on GitHub (pinned to a58a4436a3)

Solutions

  1. Set a valid, non-empty MIME type string (e.g. "application/json" or "application/proto"; use "*" for a wildcard) when calling add/RegisterMarshaler
  2. Fix the custom Marshaler's ContentType() method so it returns a concrete MIME type in all code paths
  3. Validate configured MIME types before passing them to the registry

Example fix

// before
reg.add("", myMarshaler)
// after
reg.add("application/vnd.my+json", myMarshaler)
Defensive patterns

Strategy: validation

Validate before calling

if mime == "" {
    return fmt.Errorf("marshaler requires a non-empty MIME type, got %q", mime)
}
reg.add(mime, marshaler)

Type guard

func hasContentType(m runtime.Marshaler) bool {
    return m.ContentType() != ""
}

Prevention

When it happens

Trigger: Calling Runtime(Registry).RegisterMarshaler, runtime.NewMarshalerRegistry, or marshalerRegistry.add with ("", someMarshaler), or with a Marshaler whose ContentType() method returns an empty string.

Common situations: A custom Marshaler implementation whose ContentType() returns "" when uninitialized or misconfigured; building marshaler maps programmatically from config data where a MIME type field was omitted; typos leaving a struct field empty.

Related errors


AI-assisted analysis of grpc-ecosystem/grpc-gateway@a58a4436a3 (2026-09-02). Data as JSON: /api/errors/8e08de3b6efe0af6. Report an issue: GitHub.