如何使用POSTMAN发布字符串数组?

问题描述 投票:3回答:2

我正在使用Postman将一个字符串数组发送到Web API。 Web API方法如下所示:

[HttpPost]
public async Task<IEnumerable<DocumentDTO>> GetDocuments([FromBody]IEnumerable<string> documentNames)
{
    return await _service.GetDocuments(documentNames);
}

我看到这个SO post关于如何使用Postman发送数组。以下是我发送数组的方式:enter image description here

Fiddler显示请求的内容类型是multipart/form-data;

enter image description here

但我收到错误回复:

{“Message”:“此资源不支持请求实体的媒体类型'multipart / form-data'。”,“ExceptionMessage”:“没有MediaTypeFormatter可用于读取'IEnumerable1' from content with media type 'multipart/form-data'.", "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1格式化程序,IFormatterLogger formatterLogger,CancellationToken类型的对象cancelToken)\ r \ n在System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)“}

我已经尝试将密钥设置为documentNamesdocumentNames[]documentNames[0]

我试过选择身体作为x-www-form-urlencoded。当我这样做时,API会收到一个空集合。

我试过选择一个身体作为raw。当我这样做时,API接收null作为参数。

问题

  1. 如何使用表单数据发送字符串数组?
  2. 如何使用x-www-form-urlencoded发送字符串数组?
  3. 如何将字符串数组作为原始JSON发送?
c# asp.net-web-api postman
2个回答
7
投票

将其作为JSON数组传递,然后选择下面的raw选项

enter image description here

对于API方法签名

    public void Post([FromBody] IEnumerable<string> value)
    {
      //some processing
    }

0
投票

如果你想通过params通过一个数组,你可以这样做。

use a variable with a same name

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.