微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐