babalae/better-genshin-impact · warning · ArgumentException
Headers JSON格式错误
Error message
Headers JSON格式错误
What it means
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.
Source
Thrown at BetterGenshinImpact/Core/Script/Dependence/Http.cs:80
public async Task<HttpReponse> Request(string method, string url, string? body = null, string? headersJson = null)
{
_logger.LogDebug($"[HTTP] 发送HTTP请求: {method} {url} Body: {(body != null ? body : "null")} Headers: {(headersJson != null ? headersJson : "null")}");
CheckHttpPermission(url);
var dictHeaders = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(headersJson))
{
try
{
var headers = JsonSerializer.Deserialize<Dictionary<string, string>>(headersJson);
if (headers != null)
{
dictHeaders = headers;
}
}
catch (JsonException)
{
throw new ArgumentException("Headers JSON格式错误");
}
}
// header全部小写
dictHeaders = dictHeaders.ToDictionary(kvp => kvp.Key.ToLowerInvariant(), kvp => kvp.Value);
// 提前取出来Content-Type,防止被覆盖
string contentType = "application/json";
if (dictHeaders.TryGetValue("content-type", out var ct))
{
contentType = ct;
dictHeaders.Remove("content-type");
}
// 使用HttpClient发送请求
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Clear();
foreach (var header in dictHeaders)View on GitHub (pinned to a7cb36712d)
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.
Example fix
// before — string concatenation (error-prone)
var headers = '{content-type: application/json, accept: text/html}'; // missing quotes!
http.get(url, headers);
// after — use JSON.stringify
var headers = JSON.stringify({
"content-type": "application/json",
"accept": "text/html"
});
http.get(url, headers); Defensive patterns
Strategy: try-catch
Validate before calling
// Validate headers JSON before passing to HTTP methods
// In JS script context:
// var headers = JSON.stringify({ "content-type": "application/json" });
// var parsed = JSON.parse(headers); // throws if invalid — catches errors early
// http.get(url, headers);
// In C# context:
try
{
System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(headersJson);
}
catch (System.Text.Json.JsonException)
{
throw new ArgumentException("Headers JSON is invalid");
} Try / catch
try
{
var resp = http.Get(url, headersJson);
}
catch (ArgumentException ex) when (ex.Message.Contains("Headers JSON格式错误"))
{
_logger.LogError("Headers JSON is malformed: {Headers}", headersJson);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 策略字符串不能为空
- 无效的小时值: {hour},必须是 0-24 之间的整数字符
- 无效的分钟值: {minute},必须是 0-59 之间的整数字符
- URL不能为空
- requestId cannot be empty
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/30dfd6bd21632ee7.
Report an issue: GitHub.