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

时间:2020-04-20 07:50:38

标签: c# api asp.net-web-api postman

我有一个POST API端点,即

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

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

我的请求网址是:

http://localhost:xxxxx/api/parameter1/employee

身体是:

enter image description here

因此,通过这种方式,我得到:415不支持的媒体类型错误。

如果我尝试其他方法,则可以使用API​​,但parameter2始终为空

那么,我应该如何在邮递员中传递parameter2值?

1 个答案:

答案 0 :(得分:1)

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

enter image description here

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

using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{  
    return await reader.ReadToEndAsync();
}