{"record":{"id":"30dfd6bd21632ee7","repo":"babalae/better-genshin-impact","slug":"headers-json","errorCode":null,"errorMessage":"Headers JSON格式错误","messagePattern":"Headers JSON格式错误","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"BetterGenshinImpact/Core/Script/Dependence/Http.cs","lineNumber":80,"sourceCode":"    public async Task<HttpReponse> Request(string method, string url, string? body = null, string? headersJson = null)\n    {\n        _logger.LogDebug($\"[HTTP] 发送HTTP请求: {method} {url} Body: {(body != null ? body : \"null\")} Headers: {(headersJson != null ? headersJson : \"null\")}\");\n        CheckHttpPermission(url);\n\n        var dictHeaders = new Dictionary<string, string>();\n        if (!string.IsNullOrWhiteSpace(headersJson))\n        {\n            try\n            {\n                var headers = JsonSerializer.Deserialize<Dictionary<string, string>>(headersJson);\n                if (headers != null)\n                {\n                    dictHeaders = headers;\n                }\n            }\n            catch (JsonException)\n            {\n                throw new ArgumentException(\"Headers JSON格式错误\");\n            }\n        }\n\n        // header全部小写\n        dictHeaders = dictHeaders.ToDictionary(kvp => kvp.Key.ToLowerInvariant(), kvp => kvp.Value);\n\n        // 提前取出来Content-Type，防止被覆盖\n        string contentType = \"application/json\";\n        if (dictHeaders.TryGetValue(\"content-type\", out var ct))\n        {\n            contentType = ct;\n            dictHeaders.Remove(\"content-type\");\n        }\n\n        // 使用HttpClient发送请求\n        using var httpClient = new HttpClient();\n        httpClient.DefaultRequestHeaders.Clear();\n        foreach (var header in dictHeaders)","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/Http.cs#L62-L98","documentation":"Thrown as ArgumentException when the headersJson parameter passed to an HTTP method cannot be deserialized into a Dictionary<string, string> by System.Text.Json. This occurs inside a try/catch that catches JsonException specifically — the raw JSON string is malformed, has invalid syntax, or contains values that are not strings.","triggerScenarios":"Calling http.get(url, headersJson) or http.post(url, body, headersJson) where headersJson is not valid JSON or does not deserialize to Dictionary<string,string>. Examples: trailing comma, single quotes instead of double quotes, numeric values instead of strings, or truncated JSON.","commonSituations":"Script builds headers JSON by string concatenation instead of JSON.stringify, producing invalid syntax. Header values are numbers/booleans (JSON allows them but Dictionary<string,string> requires strings). Legacy header strings with format issues. Copy-paste from browser DevTools with single-quoted keys.","solutions":["Use JSON.stringify to build the headers string: JSON.stringify({\"content-type\": \"application/json\"}).","Ensure all header values are strings: {\"authorization\": \"Bearer xyz\"} not {\"authorization\": 123}.","Validate the JSON before passing: try JSON.parse(headersJson) in the script to catch errors early.","Pass null or empty string for no headers instead of malformed JSON."],"exampleFix":"// before — string concatenation (error-prone)\nvar headers = '{content-type: application/json, accept: text/html}'; // missing quotes!\nhttp.get(url, headers);\n\n// after — use JSON.stringify\nvar headers = JSON.stringify({\n  \"content-type\": \"application/json\",\n  \"accept\": \"text/html\"\n});\nhttp.get(url, headers);","handlingStrategy":"try-catch","validationCode":"// Validate headers JSON before passing to HTTP methods\n// In JS script context:\n// var headers = JSON.stringify({ \"content-type\": \"application/json\" });\n// var parsed = JSON.parse(headers); // throws if invalid — catches errors early\n// http.get(url, headers);\n\n// In C# context:\ntry\n{\n    System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(headersJson);\n}\ncatch (System.Text.Json.JsonException)\n{\n    throw new ArgumentException(\"Headers JSON is invalid\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var resp = http.Get(url, headersJson);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Headers JSON格式错误\"))\n{\n    _logger.LogError(\"Headers JSON is malformed: {Headers}\", headersJson);\n}","preventionTips":["Always use JSON.stringify to build headers JSON in scripts.","Ensure all header values are strings, not numbers or booleans.","Validate with JSON.parse before passing to HTTP methods.","Pass null or empty string for no headers instead of invalid JSON."],"tags":["validation","http","json-parsing","headers","argument"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}