使用 Asp.net core 2.1 的 HTTP 客户端补丁方法,错误请求

Posted

技术标签:

【中文标题】使用 Asp.net core 2.1 的 HTTP 客户端补丁方法,错误请求【英文标题】:HTTP Client Patch method with Asp.net core 2.1, bad request 【发布时间】:2022-01-08 01:09:19 【问题描述】:

使用HttpClient 执行 PATCH 方法请求时出现错误请求错误

var patchDoc = new JsonPatchDocument<ClientOrganisationRequestArgs>();
patchDoc.Replace(e => e.Name, clientOrganisationRequestArgs.Name);
var serializedDoc = JsonConvert.SerializeObject(patchDoc);
var requestContent = new StringContent(serializedDoc, Encoding.UTF8, "application/json-patch+json");

var httpResponseMessage = await _httpClient.PatchAsync(
    $"api/orgs/clientOrganisationRequestArgs.ClientRefId", requestContent);

型号

public class ClientOrganisationRequestArgs

    public int ClientRefId  get; set;         
    public string Name  get; set; 
    public string DefaultTimezone => "AUSTRALIA/SYDNEY";
    public string CustomStylePath  get; set; 

请求错误时获得 400

Postman 还显示 500 内部服务器错误,正文为原始内容

授权和接受标头

_httpClient.DefaultRequestHeaders.Add(
                HeaderNames.Accept, "application/vnd.yellowfin.api-v1.3+json");
_authorizationHeader =
                $"_authorizationHeader, token=accessToken.SecurityToken";
            
            _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(
                HeaderNames.Authorization, _authorizationHeader);

【问题讨论】:

授权头和接受头是否发送正确? 根据显示的文档,它期望简单的 application/json 内容类型标头,但您有 application/json-patch+json 作为内容类型 【参考方案1】:

您有 application/json-patch+json 作为内容类型

//...

var requestContent = new StringContent(serializedDoc, Encoding.UTF8, "application/json-patch+json");

//...

但根据显示的文档,它需要简单的 application/json 内容类型标题

var requestContent = new StringContent(serializedDoc, Encoding.UTF8, "application/json");

同样基于所示文档中的示例,有效负载是一个纯 JSON 文档,看起来并不真正遵循 JSON Patch 文档格式。这可能会导致 API 无法识别请求内容。

我建议序列化有效负载以匹配给定的示例

string serializedDoc = JsonConvert.SerializeObject(clientOrganisationRequestArgs);

StringContent requestContent = new StringContent(serializedDoc, Encoding.UTF8, "application/json");

string url = $"api/orgs/clientOrganisationRequestArgs.ClientRefId";

var httpResponseMessage = await _httpClient.PatchAsync(url, requestContent);

假设

public class ClientOrganisationRequestArgs 
    [JsonProperty("clientRefId")]
    public int ClientRefId  get; set; 
    [JsonProperty("name")]
    public string Name  get; set; 
    [JsonProperty("defaultTimezone")]
    public string DefaultTimezone => "AUSTRALIA/SYDNEY";
    [JsonProperty("customStylePath")]
    public string CustomStylePath  get; set; 

【讨论】:

我收到 500 个内部服务器错误,不知道是什么原因 @SanJaisy 你能调试一下看看实际的错误/异常是什么。 500 可以是任何东西 我怎样才能用那个 httpcall 调试出什么问题。在 HttpClient 我只有 public Task PatchAsync(string requestUri, HttpContent content);没有实现,不知道怎么调试

以上是关于使用 Asp.net core 2.1 的 HTTP 客户端补丁方法,错误请求的主要内容,如果未能解决你的问题,请参考以下文章

学习 ASP.NET Core 2.1:集成测试中使用 WebApplicationFactory

如何在 asp.net core 2.1 中使用自定义消息设置状态代码?

ASP.NET Core 2.1 在视图中使用 HttpContext

ASP.NET Core 2.1 Razor 页面返回带有模型的页面

在 ASP.NET Core 2.1 中使用 cookie [重复]

如何在 ASP.NET Core 2.1 中使用自动完成输入引发模式