Nicros
Nicros

Reputation: 5183

Passing multiple arrays to get request?

I have a WebAPI 2 ApiController with the following GET method:

public IEnumerable<MyData> Get([FromUri]string[] id, [FromUri]string[] filter, int? count)

My http request looks like this:

http://localhost/myapp/api/mycontroller?id=123&id=456&filter=A&filter=B&count=5

And life is good. But here's where I'm missing something. The following query string also works:

http://localhost/myapp/api/mycontroller?id=123&id=456&count=5

Notice that the filter parameter is missing. Not a problem. But then this fails:

http://localhost/myapp/api/mycontroller?id=123&id=456&filter=A&filter=B

The count parameter seems to be required, even though it is marked as nullable. If I just add it back in with no value it works again, even if the filter or id parameters are completely left out.

This seems strange to me, what's going on?

Upvotes: 1

Views: 870

Answers (1)

EMIL ILIEV
EMIL ILIEV

Reputation: 11

  1. Make sure GET is the right method you want to use. There are limitations of the URL length. It's only 2K for IE. GET is better for some cases but it seems to me you should use POST instead.

  2. You can't submit duplicate keys in the URL. What you can do is separate multiple values for the same key with a comma "," like this:

localhost/myapp/api/mycontroller?id=123,456&filter=A,B

I hope this helps.

Upvotes: 1

Related Questions