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

我正在使用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”: “The request entity’s media type ‘multipart/form-data’
is not supported for this resource.”,“ExceptionMessage”: “No
MediaTypeFormatter is available to read an object of type
‘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 formatters,IFormatterLogger
formatterLogger,CancellationToken cancellationToken)\r\n at
System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage
request,IEnumerable`1 formatters,CancellationToken cancellationToken)” }

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

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

我试过选择一个原始的身体.当我这样做时,API接收null作为参数.

问题

>如何使用表单数据发送字符串数组?
>如何使用x-www-form-urlencoded发送字符串数组?
>如何将字符串数组作为原始JSON发送?

解决方法

将其作为JSON数组传递,然后选择如下的原始选项

enter image description here

对于API方法签名

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...