14

I am using Postman to send an array of string to a web API. The web API method looks like:

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

I saw this SO post on how to send an array using Postman. Here is how I am sending the array: enter image description here

Fiddler shows the request's content type is multipart/form-data;

enter image description here

But I am getting an error response:

{ "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, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)" }

I have tried setting the key as documentNames,documentNames[], documentNames[0].

I have tried selecting the body as x-www-form-urlencoded. When I do that, the API receives an empty collection.

I have tried selecting a body as raw. When I do that, the API receives null as a parameter.

Questions

  1. How do I send a string array using form-data?
  2. How do I send a string array using x-www-form-urlencoded?
  3. How do I send a string array as raw JSON?
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
LP13
  • 30,567
  • 53
  • 217
  • 400

3 Answers3

27

Pass it as JSON array and select the raw option like below

enter image description here

For a API method signature

    public void Post([FromBody] IEnumerable<string> value)
    {
      //some processing
    }
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Thank you , I used same idea for list of complex objects and it works fine for me – Omar Isaid Sep 19 '19 at 10:36
  • so simple and I was fiddling with it for such a long time. it came to my surprise that passing it as { value:[1,2,3] } cannot be recognized by the request handler. – Ben Dec 04 '19 at 17:11
19

if you want to initialize an array via params, you can do that.

use a variable with a same name

enter image description here

WasiF
  • 26,101
  • 16
  • 120
  • 128
7

Use same key in form remember add [] on it enter image description here

Hà Mã Tím
  • 143
  • 2
  • 3
  • adding[] works, specially when we json_encode to save it as a string in our database. if you do `categories[0]` / `categories[1]` it will create key value pairs and the value arriving at the server will not be in a single array – Manas Jan 19 '22 at 04:53