如何在邮递员的帖子请求正文中传递字符串

Dar*_*ter 11 c# asp.net-web-api postman

我有一个 POST API 端点,即

[Route("api/{parameter1}/employee")]
[HttpPost]
public Employee RegisterEmployee(string parameter1, [FromBody] string parameter2)
Run Code Online (Sandbox Code Playgroud)

现在,我想从邮递员那里使用这个 API。

我的请求网址是:

http://localhost:xxxxx/api/parameter1/employee
Run Code Online (Sandbox Code Playgroud)

身体是:

在此输入图像描述

因此,通过这种方式,我得到: 415 Unsupported Media Type 错误。

如果我尝试其他方法,那么我可以访问 API,但parameter2 始终为 null

那么,我应该如何在postman中传递parameter2值呢?

Mar*_*cik 9

尝试将内容类型设置为application/json. 值得庆幸的是,JSON 字符串输入在 ASP.NET Core 中捕获为字符串。

在此输入图像描述

或者,您可以从请求流中读取正文字符串:

using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{  
    return await reader.ReadToEndAsync();
}
Run Code Online (Sandbox Code Playgroud)