c# - API Patch 方法返回错误请求 400
问题描述
我正在使用.net core web api。在我的 API 控制器类中有PATCH
如下方法,
[HttpPatch("updateMessageTemplate/{templateId}")]
public IActionResult UpdateMessageTemplate([FromHeader] int tenantId, int templateId,[FromBody] testClass msg)
{
try
{
//Some implementation is here
return Accepted();
}
catch
{
return StatusCode(500);
}
}
testClass
如下,
public class testClass
{
public string body { get; set; }
}
我从邮递员那里调用了 API,它返回 400 BadRequest。
我在 Controller 方法中放置了断点,但它没有命中。在我[FromBody] testClass msg
从方法参数中删除断点后没有返回400
。为什么它在我使用时返回 400 [FromBody] testClass msg
?以及如何从 HTTP 客户端调用此控制器方法?
我试过了,它也返回 400 BadRequest
string serviceUrl = string.Format("{0}/notification/updateMessageTemplate/{1}", ConfigurationManager.AppSettings["LtApiUrl"], templateID);
string json = "[{\"body\":\"sample text\"}]";
HttpClient client = new HttpClient();
HttpMethod method = new HttpMethod("PATCH");
HttpRequestMessage message = new HttpRequestMessage(method, serviceUrl);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("tenantId", tenantId.ToString());
client.DefaultRequestHeaders.Add("Authorization", string.Format("bearer {0}", token));
message.Content = content;
var response = client.SendAsync(message).Result;
return response.StatusCode.ToString();
我该如何解决这个问题?请帮我。我删除了上一个问题,这是我真正的问题
更新:
我将邮递员请求更改为。
之后它的作品。但是当我通过 http 客户端代码调用它时,它提供了400 BadRequest
. 如何通过http客户端提供JSON正文正确的方式
解决方案
推荐阅读
- azure-devops - 按团队限制 Azure DevOps Boards 权限
- database - Building a permissions table with Prisma 2 and PostgreSQL to authorize users based on permission level?
- python - Requests doesnt retrieve the site after post request
- amazon-web-services - AWS WebSocket断开路由不起作用
- android - Bitmap does not scale accurately when screen size changes
- java - 删除一个链接如何从链表中删除一个节点?
- python - 如果列包含字符串,则返回列表
- xcode - 不知道如何在 SwiftUI (Xcode 12.4) 中打开地图
- elasticsearch - 如何从 kibana 获取 JSON?
- python - 如何在 Julia 中使用 Anaconda 中不可用且需要通过 pip 安装的 Python 包