{"record":{"id":"ec2103d31c944d4a","repo":"theonedev/onedev","slug":"error-parsing-media-type-0","errorCode":null,"errorMessage":"Error parsing media type '{0}'","messagePattern":"Error parsing media type '(.+?)'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/org/glassfish/jersey/message/internal/MediaTypeProvider.java","lineNumber":77,"sourceCode":"    }\n\n    @Override\n    public MediaType fromString(String header) {\n\n        throwIllegalArgumentExceptionIfNull(header, MEDIA_TYPE_IS_NULL);\n\n\t\tvar commaIndex = header.indexOf(',');\n\t\tif (commaIndex != -1) {\n\t\t\tvar colonIndex = header.indexOf(';', commaIndex);\n\t\t\tif (colonIndex != -1)\n\t\t\t\theader = header.substring(0, commaIndex) + header.substring(colonIndex);\n\t\t\telse \n\t\t\t\theader = header.substring(0, commaIndex);\n\t\t}\n        try {\n            return valueOf(HttpHeaderReader.newInstance(header));\n        } catch (ParseException ex) {\n            throw new IllegalArgumentException(\n                    \"Error parsing media type '\" + header + \"'\", ex);\n        }\n    }\n\n    /**\n     * Create a new {@link javax.ws.rs.core.MediaType} instance from a header reader.\n     *\n     * @param reader header reader.\n     * @return new {@code MediaType} instance.\n     *\n     * @throws ParseException in case of a header parsing error.\n     */\n    public static MediaType valueOf(HttpHeaderReader reader) throws ParseException {\n        // Skip any white space\n        reader.hasNext();\n\n        // Get the type\n        final String type = reader.nextToken().toString();","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/org/glassfish/jersey/message/internal/MediaTypeProvider.java#L59-L95","documentation":"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).","triggerScenarios":"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.","commonSituations":"Clients sending malformed Content-Type or Accept headers; proxies/gateways rewriting headers incorrectly; test code passing hand-built media type strings to MediaType.valueOf.","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"],"exampleFix":"// before\ncurl -X POST -H 'Content-Type: applicationjson' ...\n// after\ncurl -X POST -H 'Content-Type: application/json' ...","handlingStrategy":"validation","validationCode":"try { javax.ws.rs.core.MediaType.valueOf(headerValue); } catch (IllegalArgumentException e) { return Response.status(400).build(); }","typeGuard":"boolean isValidMediaType(String s) { try { javax.ws.rs.core.MediaType.valueOf(s); return true; } catch (IllegalArgumentException e) { return false; } }","tryCatchPattern":"try { MediaType mt = MediaType.valueOf(raw); } catch (IllegalArgumentException e) { throw new BadRequestException(\"Malformed media type: \" + raw); }","preventionTips":["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"],"tags":["jersey","http","media-type","header-parsing"],"backgroundTag":"invalid-argument-format","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}