简体   繁体   English

"ASP.net MVC 客户端使用补丁 API 获取错误请求 400 返回"

[英]ASP.net MVC clients consume patch API get Bad request 400 returns

I'm using asp.net MVC 5 to consume API that also developed in asp.net MVC.我正在使用 asp.net MVC 5 来使用也在 asp.net MVC 中开发的 API。

For POST and GET requests, I managed to make it work, except for PATCH that always get 400 bad request from web service.对于 POST 和 GET 请求,我设法使其工作,除了 PATCH 总是从 Web 服务收到 400 个错误请求。

This is what I do in my client controller:这就是我在客户端控制器中所做的:

using (HttpClient httpClient1 = new HttpClient())
{
  string apiURLGetClientApproval = "/clients/approvals?action=" + actionType;
  HttpMethod method = new HttpMethod("PATCH");
  HttpRequestMessage message = new HttpRequestMessage(method, new Uri(baseAddress + apiURLGetClientApproval));
  StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
  httpClient1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
  message.Content = content;
  var result = httpClient1.SendAsync(message).Result;
}

I have been dealing with the same exact problem for 2 days now.我已经处理同样的问题 2 天了。 I just fixed it.我刚刚修好了。 I realised that sending a PATCH request probably required some specific payload [{"op":"replace"....}] as we can tell from using PostMan.我意识到发送 PATCH 请求可能需要一些特定的有效负载 [{"op":"replace"....}],正如我们可以从使用 PostMan 中看出的那样。 However the PUT request doesn't, in fact most of the data on the business object would already be populated, so you modify what you want to change and send a PUT request instead.但是 PUT 请求不会,实际上业务对象上的大部分数据已经被填充,因此您修改要更改的内容并发送 PUT 请求。 I just did that.我就是这么做的。 I had to add the PUT action method in my controller and change the HttpClient to send a PUT request and it worked less than 5mins ago.我必须在我的控制器中添加 PUT 操作方法并更改 HttpClient 以发送 PUT 请求,并且它在不到 5 分钟前工作。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Web API HTTP 400错误请求 - ASP.NET Web API HTTP 400 Bad Request 错误请求 400 从 API 补丁方法返回 - Bad request 400 returns from API Patch method 在C#ASP.NET MVC3中获取名称包含&字符的文件时出现错误400(错误请求) - Error 400 (Bad Request) when get a file which name contains & character in C# ASP.NET MVC3 ASP.NET 核心 5,带有来自正文的参数的 HttpPut 请求始终返回 Http 400 错误请求 - ASP.NET Core 5, HttpPut request with parameters from body always returns Http 400 Bad request ASP.NET - GET 请求上的 API 400 错误 - ASP.NET - API 400 error on GET request IIS Express Asp.Net 长 URL 返回 400 Bad Request - IIS Express Asp.Net long URL returns 400 Bad Request HTTP发布返回错误400错误请求asp.net C# - Http post returns error 400 bad request asp.net c# 部署到测试服务器时,POST请求上的ASP.NET Web API“ 400错误请求” - ASP.NET Web API “400 Bad Request” on POST Request When Deploying to Test Server 尝试在ASP.NET MVC中对控制器进行Ajax调用时出现400错误的请求 - 400 Bad Request when trying Ajax call to Controller in ASP.NET MVC AD Microsoft登录400错误的请求标头太长ASP.net MVC - AD Microsoft Login 400 bad request headers too long ASP.net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM