Blankj/AndroidUtilCode · error · IllegalArgumentException

MediaType is not correct: "{}"

Error message

MediaType is not correct: "{}"

What it means

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.

Source

Thrown at lib/subutil/src/main/java/com/blankj/subutil/util/http/Request.java:91

        private Body(final String mediaType, final InputStream body) {
            this.mediaType = mediaType;
            if (body instanceof BufferedInputStream) {
                bis = (BufferedInputStream) body;
            } else {
                bis = new BufferedInputStream(body);
            }
            length = -1;
        }

        private static String getCharsetFromMediaType(String mediaType) {
            mediaType = mediaType.toLowerCase().replace(" ", "");
            int index = mediaType.indexOf("charset=");
            if (index == -1) return "utf-8";
            int st = index + 8;
            int end = mediaType.length();
            if (st >= end) {
                throw new IllegalArgumentException("MediaType is not correct: \"" + mediaType + "\"");
            }
            for (int i = st; i < end; i++) {
                char c = mediaType.charAt(i);
                if (c >= 'A' && c <= 'Z') continue;
                if (c >= 'a' && c <= 'z') continue;
                if (c >= '0' && c <= '9') continue;
                if (c == '-' && i != 0) continue;
                if (c == '+' && i != 0) continue;
                if (c == ':' && i != 0) continue;
                if (c == '_' && i != 0) continue;
                if (c == '.' && i != 0) continue;
                end = i;
                break;
            }
            String charset = mediaType.substring(st, end);
            return checkCharset(charset);
        }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Provide a real charset value, e.g. Body.create("text/plain;charset=utf-8", bytes).
  2. Omit the charset entirely — the parser returns utf-8 by default when 'charset=' is absent.
  3. Validate any dynamically-built media type: ensure that if it contains 'charset=', a non-empty token follows the '='.

Example fix

// before
Body.create("application/json;charset=", json.getBytes()); // throws

// after
Body.create("application/json;charset=utf-8", json.getBytes());
// or simply omit charset (defaults to utf-8):
Body.create("application/json", json.getBytes());
Defensive patterns

Strategy: validation

Validate before calling

private static String safeMediaType(String base, String charset) {
    if (charset == null || charset.isEmpty()) return base; // omit charset -> defaults to utf-8
    return base + ";charset=" + charset;
}
Body.create(safeMediaType("application/json", charset), bytes);

Type guard

private static boolean isValidMediaType(String mt) {
    if (mt == null) return false;
    int i = mt.toLowerCase().replace(" ", "").indexOf("charset=");
    return i == -1 || (i + 8) < mt.length();
}

Try / catch

try {
    Body.create(mediaType, bytes);
} catch (IllegalArgumentException e) {
    // mediaType ended with 'charset='; rebuild without the empty charset
    Body.create(stripCharset(mediaType), bytes);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/5ba0a3730a2d8381. Report an issue: GitHub.