c# - 如何在 postman 的帖子请求正文中传递字符串

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

我有一个 POST API 端点,即

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

现在,我想从 postman 那里点击这个 API。

我的请求网址是:
http://localhost:xxxxx/api/parameter1/employee

body 是:

enter image description here

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

如果我尝试其他方式,那么我可以访问 API,但参数 2 总是为空 .

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

最佳答案

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

enter image description here

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

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

关于c# - 如何在 postman 的帖子请求正文中传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61317515/

相关文章:

android - Google Fit API 和 ActivityRecognitionAPI 之间的区别

angularjs - Angular JS 和 Twitter API,我们如何将它们连接起来

asp.net-mvc - WebAPI 接口(interface)版本控制的推荐实践

c# - Web API 业务层架构及其职责

C# - 为什么时间和日期不保存在 mysql 数据库中?

java - 如何命名执行对象创建和更新的类

c# - 使用 EF5 更新实体

c# - CopyFromScreen 返回表单后面的任何图像

angular - 如何改变 Angular 环境

c# - 尝试创建类型为 'EmployeeController' 的 Controller 时出错。确保 Controller 有一个无参数的公共(public)构造函数