alibaba/Sentinel · error · IllegalArgumentException

charset is not allowed to be null

Error message

charset is not allowed to be null

What it means

SimpleHttpClient (the simple-http transport's heartbeat client) URL-encodes request parameters using java.net.URLEncoder, which requires a Charset. encodeRequestParams throws IllegalArgumentException("charset is not allowed to be null") if the charset argument is null, since URLEncoder.encode(key, charset.name()) would NPE otherwise. The charset normally comes from SimpleHttpRequest.getCharset(), defaulting to UTF-8 in the request builder.

Source

Thrown at sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java:163

    }

    private String getStatusLine(RequestMethod type, String requestPath) {
        if (type == RequestMethod.POST) {
            return "POST " + requestPath + " HTTP/1.1";
        }
        return "GET " + requestPath + " HTTP/1.1";
    }

    /**
     * Encode and get the URL request parameters.
     *
     * @param paramsMap pair of parameters
     * @param charset   charset
     * @return encoded request parameters, or empty string ("") if no parameters are provided
     */
    private String encodeRequestParams(Map<String, String> paramsMap, Charset charset) {
        if (charset == null) {
            throw new IllegalArgumentException("charset is not allowed to be null");
        }
        if (paramsMap == null || paramsMap.isEmpty()) {
            return "";
        }
        try {
            StringBuilder paramsBuilder = new StringBuilder();
            for (Entry<String, String> entry : paramsMap.entrySet()) {
                if (entry.getKey() == null || entry.getValue() == null) {
                    continue;
                }
                paramsBuilder.append(URLEncoder.encode(entry.getKey(), charset.name()))
                    .append("=")
                    .append(URLEncoder.encode(entry.getValue(), charset.name()))
                    .append("&");
            }
            if (paramsBuilder.length() > 0) {
                // Remove the last '&'.
                paramsBuilder.delete(paramsBuilder.length() - 1, paramsBuilder.length());

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Do not call setCharset(null); rely on the request's default (UTF-8) or set Charset.forName("UTF-8") explicitly
  2. If the charset comes from config, validate it at startup: resolve the name once and fall back to UTF-8 for the request
  3. Keep using the provided request builders (SimpleHttpRequest wrapper methods) which populate the charset

Example fix

// before
Charset cs = parseCharset(config.get("charset")); // null when key absent
request.setCharset(cs);

// after
Charset cs = parseCharset(config.get("charset"));
request.setCharset(cs != null ? cs : Charset.forName("UTF-8"));
Defensive patterns

Strategy: validation

Validate before calling

Charset cs = resolveCharset(configName);
request.setCharset(cs != null ? cs : Charset.forName("UTF-8"));

Prevention

When it happens

Trigger: Building a SimpleHttpRequest and calling setCharset(null) (or constructing with a null charset through custom code) before SimpleHttpClient.get/post encodes the query string.

Common situations: Custom heartbeat senders or tests that explicitly set a charset from configuration that failed to load; refactoring that made the charset optional; code copying SimpleHttpRequest usage without the default-builder path.

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/d82ade42c114358b. Report an issue: GitHub.