theonedev/onedev · error · IllegalArgumentException
Error parsing media type '{0}'
Error message
Error parsing media type '{0}' What it means
Jersey's MediaTypeProvider implements the ParamConverter-style fromString for javax.ws.rs.core.MediaType. It parses a header string with HttpHeaderReader and rethrows any ParseException as IllegalArgumentException with message "Error parsing media type '{0}'". This happens when the raw string is not a syntactically valid media type (type/subtype with optional parameters).
Source
Thrown at server-core/src/main/java/org/glassfish/jersey/message/internal/MediaTypeProvider.java:77
}
@Override
public MediaType fromString(String header) {
throwIllegalArgumentExceptionIfNull(header, MEDIA_TYPE_IS_NULL);
var commaIndex = header.indexOf(',');
if (commaIndex != -1) {
var colonIndex = header.indexOf(';', commaIndex);
if (colonIndex != -1)
header = header.substring(0, commaIndex) + header.substring(colonIndex);
else
header = header.substring(0, commaIndex);
}
try {
return valueOf(HttpHeaderReader.newInstance(header));
} catch (ParseException ex) {
throw new IllegalArgumentException(
"Error parsing media type '" + header + "'", ex);
}
}
/**
* Create a new {@link javax.ws.rs.core.MediaType} instance from a header reader.
*
* @param reader header reader.
* @return new {@code MediaType} instance.
*
* @throws ParseException in case of a header parsing error.
*/
public static MediaType valueOf(HttpHeaderReader reader) throws ParseException {
// Skip any white space
reader.hasNext();
// Get the type
final String type = reader.nextToken().toString();View on GitHub (pinned to d44925c47c)
Solutions
- Inspect the raw request header and fix the client so it sends a valid type/substring media type
- Test the string with MediaType.valueOf locally to reproduce and correct it
- Add a request filter to validate/reject malformed headers with a clear 400 before resource matching
Example fix
// before curl -X POST -H 'Content-Type: applicationjson' ... // after curl -X POST -H 'Content-Type: application/json' ...
Defensive patterns
Strategy: validation
Validate before calling
try { javax.ws.rs.core.MediaType.valueOf(headerValue); } catch (IllegalArgumentException e) { return Response.status(400).build(); } Type guard
boolean isValidMediaType(String s) { try { javax.ws.rs.core.MediaType.valueOf(s); return true; } catch (IllegalArgumentException e) { return false; } } Try / catch
try { MediaType mt = MediaType.valueOf(raw); } catch (IllegalArgumentException e) { throw new BadRequestException("Malformed media type: " + raw); } Prevention
- Fix clients sending malformed Content-Type/Accept headers
- Add header validation at a request filter to fail fast with 400
- Test media-type strings with MediaType.valueOf in unit tests
When it happens
Trigger: Jersey encounters an HTTP header value such as Content-Type or Accept that cannot be parsed as a media type, e.g. "text/plain; charset=UTF-8;" malformed, missing subtype, or stray characters; also via MediaType.valueOf(...) on a bad string.
Common situations: Clients sending malformed Content-Type or Accept headers; proxies/gateways rewriting headers incorrectly; test code passing hand-built media type strings to MediaType.valueOf.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Http request failed (url: %s, status code: %d, error message
- Http request failed (status: %s)
- Exception obtaining parameters
- Unauthenticated
- Project not specified
AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06).
Data as JSON: /api/errors/ec2103d31c944d4a.
Report an issue: GitHub.