{"record":{"id":"b7b81682b497e774","repo":"didi/DoKit","slug":"unexpected-char-04x-at-d-in-s-value-s","errorCode":null,"errorMessage":"Unexpected char %#04x at %d in %s value: %s","messagePattern":"Unexpected char %#04x at (.+?) in (.+?) value: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/common/CommonHeaders.java","lineNumber":292,"sourceCode":"      addLenient(name, value);\n      return this;\n    }\n\n    private void checkNameAndValue(String name, String value) {\n      if (name == null) throw new NullPointerException(\"name == null\");\n      if (name.isEmpty()) throw new IllegalArgumentException(\"name is empty\");\n      for (int i = 0, length = name.length(); i < length; i++) {\n        char c = name.charAt(i);\n        if (c <= '\\u0020' || c >= '\\u007f') {\n          throw new IllegalArgumentException(format(\n              \"Unexpected char %#04x at %d in header name: %s\", (int) c, i, name));\n        }\n      }\n      if (value == null) throw new NullPointerException(\"value == null\");\n      for (int i = 0, length = value.length(); i < length; i++) {\n        char c = value.charAt(i);\n        if ((c <= '\\u001f' && c != '\\t') || c >= '\\u007f') {\n          throw new IllegalArgumentException(format(\n              \"Unexpected char %#04x at %d in %s value: %s\", (int) c, i, name, value));\n        }\n      }\n    }\n\n    /** Equivalent to {@code build().get(name)}, but potentially faster. */\n    public String get(String name) {\n      for (int i = namesAndValues.size() - 2; i >= 0; i -= 2) {\n        if (name.equalsIgnoreCase(namesAndValues.get(i))) {\n          return namesAndValues.get(i + 1);\n        }\n      }\n      return null;\n    }\n\n    public CommonHeaders build() {\n      return new CommonHeaders(this);\n    }","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/common/CommonHeaders.java#L274-L310","documentation":"Thrown by CommonHeaders.Builder.checkNameAndValue when a header VALUE contains a control character other than tab (<= U+001F) or a non-ASCII character (>= U+007F) at position i. Unlike names, values may contain tabs and spaces, but raw control or non-ASCII bytes must be encoded first.","triggerScenarios":"builder.add(\"X-Meta\", \"line1\\nline2\") (newline in value), a value containing '\\0', or a value with a CJK/accented character such as add(\"X-Note\", \"失败\") passed unencoded.","commonSituations":"JSON or binary payloads stuffed into custom header values without encoding; localized error messages copied into headers; values read from files/network that include CR/LF (also a header-injection vector).","solutions":["URL-encode (percent-encode) or Base64 any non-ASCII header value before add()","Strip CR/LF and other control chars from values assembled from free text (also guards header injection)","Log the failing name/value on IllegalArgumentException to identify which header carries the bad byte"],"exampleFix":"// before\nbuilder.add(\"X-Note\", note); // note contains '\\n' or non-ASCII\n\n// after\nbuilder.add(\"X-Note\", URLEncoder.encode(note, \"UTF-8\"));","handlingStrategy":"validation","validationCode":"static boolean isValidHeaderValue(String v) {\n  if (v == null) return false;\n  for (int i = 0; i < v.length(); i++) {\n    char c = v.charAt(i);\n    if ((c <= '\\u001f' && c != '\\t') || c >= '\\u007f') return false;\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["URL-encode or Base64 non-ASCII values before putting them in headers","Strip CR/LF from free-text values to prevent both this error and header injection"],"tags":["android","network","headers","validation","encoding"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}