{"record":{"id":"91cdca7e7468528c","repo":"code4craft/webmagic","slug":"illegal-encoding","errorCode":null,"errorMessage":"illegal encoding ","messagePattern":"illegal encoding ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java","lineNumber":71,"sourceCode":"    }\n\n    public void setBody(byte[] body) {\n        this.body = body;\n    }\n\n    public void setContentType(String contentType) {\n        this.contentType = contentType;\n    }\n\n    public void setEncoding(String encoding) {\n        this.encoding = encoding;\n    }\n\n    public static HttpRequestBody json(String json, String encoding) {\n        try {\n            return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);\n        } catch (UnsupportedEncodingException e) {\n            throw new IllegalArgumentException(\"illegal encoding \" + encoding, e);\n        }\n    }\n\n    public static HttpRequestBody xml(String xml, String encoding) {\n        try {\n            return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);\n        } catch (UnsupportedEncodingException e) {\n            throw new IllegalArgumentException(\"illegal encoding \" + encoding, e);\n        }\n    }\n\n    public static HttpRequestBody custom(byte[] body, String contentType, String encoding) {\n        return new HttpRequestBody(body, contentType, encoding);\n    }\n\n    public static HttpRequestBody form(Map<String,Object> params, String encoding){\n        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());\n        for (Map.Entry<String, Object> entry : params.entrySet()) {","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java#L53-L89","documentation":"HttpRequestBody.json() encodes the JSON string with the given charset; if the charset name is not supported on the JVM, UnsupportedEncodingException is caught and rethrown as IllegalArgumentException('illegal encoding ' + encoding).","triggerScenarios":"Calling HttpRequestBody.json(json, \"utf8\") or any misspelled charset ('utf-8 ' with trailing space, 'utf_8'); charset not present in the JVM's supported list.","commonSituations":"Hardcoding charset names from config or headers without canonicalization; typos like 'utf8' vs 'UTF-8' on strict JVM configurations; reading encoding from an HTTP Content-Type header.","solutions":["Use a canonical constant: StandardCharsets.UTF_8.name()","Normalize/trim the charset and validate with Charset.isSupported(encoding) before calling","Fall back to UTF-8 when the encoding is unknown"],"exampleFix":"// before\nHttpRequestBody.json(json, \"utf_8\");\n// after\nString encoding = \"utf_8\";\nif (!Charset.isSupported(encoding)) encoding = StandardCharsets.UTF_8.name();\nHttpRequestBody.json(json, encoding);","handlingStrategy":"validation","validationCode":"if (!java.nio.charset.Charset.isSupported(encoding)) encoding = java.nio.charset.StandardCharsets.UTF_8.name();","typeGuard":null,"tryCatchPattern":"try { HttpRequestBody.json(json, encoding); } catch (IllegalArgumentException e) { HttpRequestBody.json(json, \"UTF-8\"); }","preventionTips":["Use StandardCharsets constants, never hand-written charset strings","Canonicalize charsets from headers/config before use","Strip whitespace and check spelling ('UTF-8', not 'utf_8')"],"tags":["encoding","charset","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}