React通过Fetch API向ASP.NET Core 6后端发送PATCH请求失败排查
React Fetch发送PATCH请求到ASP.NET Core 6 Web API返回400 Bad Request问题
我在React前端通过Fetch API向ASP.NET Core 6 Web API后端发送PATCH请求时,收到400 Bad Request响应。我封装了自定义Fetch方法用于添加授权等额外头信息,POST、PUT、GET等其他请求均正常工作。
封装的Fetch方法代码
async function customFetch(url: string, method: RequestType, body?: string) : Promise<Response> { const requestHeaders: HeadersInit = new Headers(); if (method == RequestType.Patch) { requestHeaders.set('Content-Type', 'application/json-patch+json; charset=UTF-8'); } ... // 其他代码块 return await fetch(url, { body: body, method: method, headers: requestHeaders }) }
组件内调用方式
const response = await requestHandler.sendRequest(config.apiurl, RequestType.Patch, { isActive: false }, id);
我已对请求体使用JSON.stringify处理,也曾尝试按JSON Patch的op、path格式发送请求体,但均无效。服务器返回响应如下:
服务器错误响应
{"errors":{"":["The JSON patch document was malformed and could not be parsed."],"patchDocument":["The patchDocument field is required."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-00e44924ab187f4c22fe894139fe2bb5-ac73a46d6b4182e9-00"}
后端Web API控制器代码
后端调试器未触发,推测异常出现在中间件中:
[HttpPatch("{id}")] public async Task<ActionResult> PartiallyUpdate(Guid id, JsonPatchDocument<RequestDto> patchDocument) { await _appService.PartiallyUpdateAsync(id, patchDocument); return NoContent(); }
请问发送PATCH请求是否需要安装npm包?
内容的提问来源于stack exchange,提问作者Melih Çoban
相关产品推荐
相关产品推荐

