alibaba/nacos · error · IllegalArgumentException

MediaType must not be empty

Error message

MediaType must not be empty

What it means

Thrown by MediaType.valueOf(String contentType) when contentType is null or empty (StringUtils.isEmpty). The parser needs at least a type token to split on ';', so it refuses to construct a MediaType from nothing. It is an IllegalArgumentException fired before any string splitting.

Source

Thrown at common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java:73

    /**
     * content type.
     */
    private final String type;
    
    /**
     * content type charset.
     */
    private final String charset;
    
    /**
     * Parse the given String contentType into a {@code MediaType} object.
     *
     * @param contentType mediaType
     * @return MediaType
     */
    public static MediaType valueOf(String contentType) {
        if (StringUtils.isEmpty(contentType)) {
            throw new IllegalArgumentException("MediaType must not be empty");
        }
        String[] values = contentType.split(";");
        String charset = Constants.ENCODE;
        for (String value : values) {
            if (value.startsWith("charset=")) {
                charset = value.substring("charset=".length());
            }
        }
        return new MediaType(values[0], charset);
    }
    
    /**
     * Use the given contentType and charset to assemble into a {@code MediaType} object.
     *
     * @param contentType contentType
     * @param charset charset
     * @return MediaType
     */

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Default to a sensible content type before parsing: String ct = StringUtils.isBlank(raw) ? MediaType.APPLICATION_JSON : raw; then MediaType.valueOf(ct).
  2. Skip media-type negotiation entirely when the header is missing instead of forcing a parse.
  3. Validate the source of contentType (e.g. assert the HTTP response actually carried Content-Type) upstream of the call.
  4. If contentType is configuration-driven, fail config loading early with a clear message rather than letting it reach valueOf.

Example fix

// before
String ct = response.getHeader("Content-Type");
MediaType type = MediaType.valueOf(ct); // throws when header absent

// after
String ct = response.getHeader("Content-Type");
MediaType type = MediaType.valueOf(
    StringUtils.isBlank(ct) ? MediaType.APPLICATION_JSON : ct);
Defensive patterns

Strategy: validation

Validate before calling

String ct = response.getHeader("Content-Type");
if (StringUtils.isBlank(ct)) {
    ct = MediaType.APPLICATION_JSON; // or skip negotiation
}
MediaType type = MediaType.valueOf(ct);

Type guard

boolean isParsableMediaType(String contentType) {
    return StringUtils.isNotEmpty(contentType) && contentType.contains("/");
}

Prevention

When it happens

Trigger: Calling MediaType.valueOf(contentType) where contentType came from an HTTP response header that was absent (null) or blank; passing request.getHeader("Content-Type") when no Content-Type was sent; defaulting a content-type field to "" and forwarding it.

Common situations: Upstream service omitted the Content-Type header; a reverse proxy stripped it; a config value like nacos.client.contentType left empty; JSON body sent without setting the content type so the negotiated type resolves to empty.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/781e383e62fa123e. Report an issue: GitHub.