+-
如何在postman的post请求中传递字符串?

我有一个POST API端点,也就是说,我有一个POST API端点。

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

现在,我想从postman打这个API。

我的请求URL是。

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

而body是:

enter image description here

所以,在这种情况下,我得到的是: 415 Unsupported Media Type错误.

如果我尝试其他的方式,那么我就可以点击API。但参数2总是以null的形式出现.

那么,我应该如何在postman中传递参数2的值?

1
投票

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

enter image description here

另外,你也可以从请求流中读取body字符串。

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