{"record":{"id":"f1005d01a1366870","repo":"JeffreySu/WeiXinMPSDK","slug":"data-null","errorCode":null,"errorMessage":"data 不能为 null！","messagePattern":"data 不能为 null！","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs","lineNumber":232,"sourceCode":"            if (timeOut <= 0 && timeOut != Timeout.Infinite)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeOut), \"超时时间必须大于 0，或使用 Timeout.Infinite。 \");\n            }\n\n            using var request = new HttpRequestMessage(GetHttpMethod(requestMethod), url);\n\n            switch (requestMethod)\n            {\n                case ApiRequestMethod.GET:\n                case ApiRequestMethod.DELETE:\n                    WeixinTrace.Log(url);\n                    break;\n                case ApiRequestMethod.POST:\n                case ApiRequestMethod.PUT:\n                case ApiRequestMethod.PATCH:\n                    if (checkDataNotNull)\n                    {\n                        _ = data ?? throw new ArgumentNullException($\"{nameof(data)} 不能为 null！\");\n                    }\n\n                    string jsonString = data != null\n                        ? data.ToJson(false, RequestJsonSerializerSettings)\n                        : \"\";\n                    WeixinTrace.SendApiPostDataLog(url, jsonString);\n                    request.Content = new StringContent(jsonString, Encoding.UTF8, mediaType: \"application/json\");\n                    break;\n                default:\n                    throw new ArgumentOutOfRangeException(nameof(requestMethod));\n            }\n\n            using var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);\n            if (timeOut != Timeout.Infinite)\n            {\n                timeoutCancellationTokenSource.CancelAfter(timeOut);\n            }\n","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs#L214-L250","documentation":"Inside TenPayApiRequest.GetHttpResponseMessageAsync, after validating timeOut and building the HttpRequestMessage, the method branches on the request method; a null data (request body) is rejected before the request is sent. The exception fires when a caller requests a POST/PUT-style API call without supplying the JSON object to serialize as the body — WeChat Pay V3 endpoints require a payload for these methods, so a null body is invalid.","triggerScenarios":"Calling PostAsync/PutAsync/PatchAsync with data == null (e.g. an empty request DTO not constructed, or a LINQ/serialization result that returned null) while leaving checkDataNotNull at its default true.","commonSituations":"Sending POST endpoints that technically accept no body but calling the method without an object; a null model from a failed mapping step.","solutions":["Pass an empty object (new { }) or the actual request DTO for POST/PUT/PATCH calls","Set checkDataNotNull: false if the endpoint genuinely accepts an empty body","Check that your DTO construction/mapping does not return null"],"exampleFix":"// before\nawait tenPayApiRequest.PostAsync(url, null);\n// after\nawait tenPayApiRequest.PostAsync(url, new { }); // or the required request DTO","handlingStrategy":"validation","validationCode":"if (data == null && checkDataNotNull) data = new { }; // or throw early with context","typeGuard":null,"tryCatchPattern":"try { await request.PostAsync(url, data); }\ncatch (ArgumentNullException ex) when (ex.ParamName?.StartsWith(\"data\") == true) { log.Error(\"POST 请求 body 为空\", ex); throw; }","preventionTips":["Always pass a request DTO (even empty new { }) for POST/PUT/PATCH","Pass checkDataNotNull:false only for endpoints documented to accept empty bodies","Map/serialize request models defensively so nulls don't propagate"],"tags":["wechat-pay","null-argument","http-post"],"backgroundTag":"null-argument","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}