{"record":{"id":"781e383e62fa123e","repo":"alibaba/nacos","slug":"mediatype-must-not-be-empty","errorCode":null,"errorMessage":"MediaType must not be empty","messagePattern":"MediaType must not be empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java","lineNumber":73,"sourceCode":"    /**\n     * content type.\n     */\n    private final String type;\n    \n    /**\n     * content type charset.\n     */\n    private final String charset;\n    \n    /**\n     * Parse the given String contentType into a {@code MediaType} object.\n     *\n     * @param contentType mediaType\n     * @return MediaType\n     */\n    public static MediaType valueOf(String contentType) {\n        if (StringUtils.isEmpty(contentType)) {\n            throw new IllegalArgumentException(\"MediaType must not be empty\");\n        }\n        String[] values = contentType.split(\";\");\n        String charset = Constants.ENCODE;\n        for (String value : values) {\n            if (value.startsWith(\"charset=\")) {\n                charset = value.substring(\"charset=\".length());\n            }\n        }\n        return new MediaType(values[0], charset);\n    }\n    \n    /**\n     * Use the given contentType and charset to assemble into a {@code MediaType} object.\n     *\n     * @param contentType contentType\n     * @param charset charset\n     * @return MediaType\n     */","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java#L55-L91","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nString ct = response.getHeader(\"Content-Type\");\nMediaType type = MediaType.valueOf(ct); // throws when header absent\n\n// after\nString ct = response.getHeader(\"Content-Type\");\nMediaType type = MediaType.valueOf(\n    StringUtils.isBlank(ct) ? MediaType.APPLICATION_JSON : ct);","handlingStrategy":"validation","validationCode":"String ct = response.getHeader(\"Content-Type\");\nif (StringUtils.isBlank(ct)) {\n    ct = MediaType.APPLICATION_JSON; // or skip negotiation\n}\nMediaType type = MediaType.valueOf(ct);","typeGuard":"boolean isParsableMediaType(String contentType) {\n    return StringUtils.isNotEmpty(contentType) && contentType.contains(\"/\");\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["http-client","media-type","validation","argument"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}