{"record":{"id":"5ba0a3730a2d8381","repo":"Blankj/AndroidUtilCode","slug":"mediatype-is-not-correct","errorCode":null,"errorMessage":"MediaType is not correct: \"{}\"","messagePattern":"MediaType is not correct: \"(.+?)\"","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/subutil/src/main/java/com/blankj/subutil/util/http/Request.java","lineNumber":91,"sourceCode":"\n        private Body(final String mediaType, final InputStream body) {\n            this.mediaType = mediaType;\n            if (body instanceof BufferedInputStream) {\n                bis = (BufferedInputStream) body;\n            } else {\n                bis = new BufferedInputStream(body);\n            }\n            length = -1;\n        }\n\n        private static String getCharsetFromMediaType(String mediaType) {\n            mediaType = mediaType.toLowerCase().replace(\" \", \"\");\n            int index = mediaType.indexOf(\"charset=\");\n            if (index == -1) return \"utf-8\";\n            int st = index + 8;\n            int end = mediaType.length();\n            if (st >= end) {\n                throw new IllegalArgumentException(\"MediaType is not correct: \\\"\" + mediaType + \"\\\"\");\n            }\n            for (int i = st; i < end; i++) {\n                char c = mediaType.charAt(i);\n                if (c >= 'A' && c <= 'Z') continue;\n                if (c >= 'a' && c <= 'z') continue;\n                if (c >= '0' && c <= '9') continue;\n                if (c == '-' && i != 0) continue;\n                if (c == '+' && i != 0) continue;\n                if (c == ':' && i != 0) continue;\n                if (c == '_' && i != 0) continue;\n                if (c == '.' && i != 0) continue;\n                end = i;\n                break;\n            }\n            String charset = mediaType.substring(st, end);\n            return checkCharset(charset);\n        }\n","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/subutil/src/main/java/com/blankj/subutil/util/http/Request.java#L73-L109","documentation":"Request.Body.getCharsetFromMediaType throws IllegalArgumentException when the media type string contains 'charset=' at the very end with no value after it (st >= end). The parser finds the charset token but the string terminates immediately after the '=', leaving no charset name to extract.","triggerScenarios":"Passing a media type ending in 'charset=' to Body.create, e.g. Body.create(\"text/plain;charset=\", bytes), or building a Content-Type string that concatenates an empty charset variable: \"application/json;charset=\" + charset where charset is empty.","commonSituations":"Constructing a Content-Type header from a user/field value that is blank; trailing 'charset=' left by a templating bug; copy-paste of a media type missing its value after the equals sign.","solutions":["Provide a real charset value, e.g. Body.create(\"text/plain;charset=utf-8\", bytes).","Omit the charset entirely — the parser returns utf-8 by default when 'charset=' is absent.","Validate any dynamically-built media type: ensure that if it contains 'charset=', a non-empty token follows the '='."],"exampleFix":"// before\nBody.create(\"application/json;charset=\", json.getBytes()); // throws\n\n// after\nBody.create(\"application/json;charset=utf-8\", json.getBytes());\n// or simply omit charset (defaults to utf-8):\nBody.create(\"application/json\", json.getBytes());","handlingStrategy":"validation","validationCode":"private static String safeMediaType(String base, String charset) {\n    if (charset == null || charset.isEmpty()) return base; // omit charset -> defaults to utf-8\n    return base + \";charset=\" + charset;\n}\nBody.create(safeMediaType(\"application/json\", charset), bytes);","typeGuard":"private static boolean isValidMediaType(String mt) {\n    if (mt == null) return false;\n    int i = mt.toLowerCase().replace(\" \", \"\").indexOf(\"charset=\");\n    return i == -1 || (i + 8) < mt.length();\n}","tryCatchPattern":"try {\n    Body.create(mediaType, bytes);\n} catch (IllegalArgumentException e) {\n    // mediaType ended with 'charset='; rebuild without the empty charset\n    Body.create(stripCharset(mediaType), bytes);\n}","preventionTips":["Always pair 'charset=' with a real value, or omit it (defaults to utf-8).","When building Content-Type dynamically, guard empty charset variables.","Validate media type strings before handing them to Body.create."],"tags":["android","http","media-type","argument","charset"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}