{"record":{"id":"d82ade42c114358b","repo":"alibaba/Sentinel","slug":"charset-is-not-allowed-to-be-null","errorCode":null,"errorMessage":"charset is not allowed to be null","messagePattern":"charset is not allowed to be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java","lineNumber":163,"sourceCode":"    }\n\n    private String getStatusLine(RequestMethod type, String requestPath) {\n        if (type == RequestMethod.POST) {\n            return \"POST \" + requestPath + \" HTTP/1.1\";\n        }\n        return \"GET \" + requestPath + \" HTTP/1.1\";\n    }\n\n    /**\n     * Encode and get the URL request parameters.\n     *\n     * @param paramsMap pair of parameters\n     * @param charset   charset\n     * @return encoded request parameters, or empty string (\"\") if no parameters are provided\n     */\n    private String encodeRequestParams(Map<String, String> paramsMap, Charset charset) {\n        if (charset == null) {\n            throw new IllegalArgumentException(\"charset is not allowed to be null\");\n        }\n        if (paramsMap == null || paramsMap.isEmpty()) {\n            return \"\";\n        }\n        try {\n            StringBuilder paramsBuilder = new StringBuilder();\n            for (Entry<String, String> entry : paramsMap.entrySet()) {\n                if (entry.getKey() == null || entry.getValue() == null) {\n                    continue;\n                }\n                paramsBuilder.append(URLEncoder.encode(entry.getKey(), charset.name()))\n                    .append(\"=\")\n                    .append(URLEncoder.encode(entry.getValue(), charset.name()))\n                    .append(\"&\");\n            }\n            if (paramsBuilder.length() > 0) {\n                // Remove the last '&'.\n                paramsBuilder.delete(paramsBuilder.length() - 1, paramsBuilder.length());","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java#L145-L181","documentation":"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.","triggerScenarios":"Building a SimpleHttpRequest and calling setCharset(null) (or constructing with a null charset through custom code) before SimpleHttpClient.get/post encodes the query string.","commonSituations":"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.","solutions":["Do not call setCharset(null); rely on the request's default (UTF-8) or set Charset.forName(\"UTF-8\") explicitly","If the charset comes from config, validate it at startup: resolve the name once and fall back to UTF-8 for the request","Keep using the provided request builders (SimpleHttpRequest wrapper methods) which populate the charset"],"exampleFix":"// before\nCharset cs = parseCharset(config.get(\"charset\")); // null when key absent\nrequest.setCharset(cs);\n\n// after\nCharset cs = parseCharset(config.get(\"charset\"));\nrequest.setCharset(cs != null ? cs : Charset.forName(\"UTF-8\"));","handlingStrategy":"validation","validationCode":"Charset cs = resolveCharset(configName);\nrequest.setCharset(cs != null ? cs : Charset.forName(\"UTF-8\"));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not call setCharset(null); omit the call to keep the UTF-8 default","Resolve configured charset names once at startup and reject unknown names early"],"tags":["sentinel","transport","http","charset","null-check"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}