我有一个 POST API 端点,即
[Route("api/{parameter1}/employee")]
[HttpPost]
public Employee RegisterEmployee(string parameter1, [FromBody] string parameter2)
现在,我想从 postman 那里点击这个 API。
我的请求网址是:
http://localhost:xxxxx/api/parameter1/employee
body 是:
所以,通过这种方式我得到:415 Unsupported Media Type 错误。
如果我尝试其他方式,那么我可以访问 API,但参数 2 总是为空 .
那么,我应该如何在 postman 中传递 parameter2 值?
最佳答案
尝试将内容类型设置为 application/json
.幸运的是,JSON 字符串输入在 ASP.NET Core 中捕获为字符串。
或者,您可以从请求流中读取正文字符串:
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
return await reader.ReadToEndAsync();
}
关于c# - 如何在 postman 的帖子请求正文中传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61317515/