【问题标题】:Call asp.net core web api from PostMan从 PostMan 调用 asp.net core web api
【发布时间】:2019-04-27 00:38:14
【问题描述】:

我正在尝试从 PostMan 调用以下函数(asp.net web api 核心):

[HttpPost]
public InfluencerSearchResultWithFacets Post(string q, string group, List<string> subGroups)
{
   return GetSearchResult("",null,null);
}

但我收到以下错误: 需要一个非空的请求正文

我已经像这样设置 PostMan:

我也尝试添加到正文:

【问题讨论】:

  • 你在做HttpPost,你应该把参数放在body里,而不是放在url里。
  • 或标题...
  • 您也可以尝试使用[FromUri] 标记您的参数。
  • 我尝试了所有组合。还将参数放入正文中。我也添加了。我在 30 秒内将图像添加到主帖。
  • 我现在尝试在所有选项卡中添加它:D 但是添加到正文时出现另一个错误。值不能为空吗?

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


【解决方案1】:

所以你可以创建一个类似的模型

public class Model
{
  public string q { get; set; }
  public string group { get; set; }
  public List<string>subGroups { get; set; }
}

并使用它

[HttpPost]
public InfluencerSearchResultWithFacets Post([FromBody] Model model)
{
   return GetSearchResult("",null,null);
}

这是如果你适合 Json 格式。 您也可以在 URL 中保留一些参数,并将其他参数作为正文传递,例如

[HttpPost]
public InfluencerSearchResultWithFacets Post([FromUri]string q, [FromUri]string group, [FromBody]List<string> subGroups)
{
   return GetSearchResult("",null,null);
}

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2018-12-30
    • 2018-03-15
    • 2021-09-16
    • 2017-01-17
    相关资源
    最近更新 更多