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
- Default to a sensible content type before parsing: String ct = StringUtils.isBlank(raw) ? MediaType.APPLICATION_JSON : raw; then MediaType.valueOf(ct).
- Skip media-type negotiation entirely when the header is missing instead of forcing a parse.
- Validate the source of contentType (e.g. assert the HTTP response actually carried Content-Type) upstream of the call.
- 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
- Always coalesce upstream Content-Type to a non-empty default before parsing.
- Log when a negotiated content type was missing so config gaps surface early.
- Do not pass user-controlled raw strings straight into MediaType.valueOf.
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
- list size must be a multiple of 2
- 20002
- Agent Version mediaType must be {AGENT_VERSION_MEDIA_TYPE}
- Unsupported http method : {name}
- httpClientFactory is null
AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14).
Data as JSON: /api/errors/781e383e62fa123e.
Report an issue: GitHub.