1

I need to send and receive an IList to my Web API:

API Method:

public IList<int> GetKeywordIdsFromIds([FromUri]List<int> ids)
{
     // this methods retuns null or an IList<int>
     return _iKeywordService.GetKeywordIdsFromIKeywordIds(ids);
}

Call method:

public List<int> GetKeywordIdsFromIds(IList<int> iKeywordIds)
{
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(API_BASE_URL);
            client.DefaultRequestHeaders.Accept.Clear();

            return client.GetAsync(url + iKeywordIds).Result.Content.ReadAsAsync<List<int>>().Result;
        }
}

I get the error:

Exception:Thrown: "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'text/html'." (System.Net.Http.UnsupportedMediaTypeException)

EDIT:

I don't know if this helps but I get this from IIS Log File:

2014-03-31 16:11:31 127.0.0.1 GET /api/ikeyword/getkeywordidsfromids/System.Collections.Generic.List`1[System.Int32] - 80 - 127.0.0.1 - 404 0 2 1

EDIT2:

Default route:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • See this post, how to add parameters to your code: http://stackoverflow.com/questions/217070/how-do-i-add-query-parameters-to-a-getmethod-using-java-commons-httpclient – Felipe Oriani Mar 31 '14 at 15:00
  • Hi thanks but I don't see any example for an IList – Patrick Mar 31 '14 at 15:05
  • where do you put `List ids` ? You leave it in the body or uri ? – cat916 Mar 31 '14 at 15:29
  • @minhcat_vo Hi thanks, sorry but I don't understand your question – Patrick Mar 31 '14 at 15:38
  • you leave your `List ids` in your body message or in url ? – cat916 Mar 31 '14 at 15:41
  • Are you saying the [FromUri] property in the API method? If so it's in URL – Patrick Mar 31 '14 at 15:44
  • I think what he is asking is whether or not the list of IDs is part of the url (http://domain/api/controller/list/{list}) or if the list of ints is written to the body. It is better to put this data in the body than as part of the url, since you don't really know how many ids are going to be passed. – bsayegh Mar 31 '14 at 15:49
  • 1
    It's a call between a ConsoleApp and a MVC Web App so the information is passed as you can see in the question. I really don't know if it's what he is asking. Sorry...I think the problem is in the call "url + iKeywordIds" because if I try with url + "/1" it works fine – Patrick Mar 31 '14 at 15:53
  • If you tried to call /1/2/3 . It would call your action. – cat916 Mar 31 '14 at 16:28
  • I updated the question with the default api route – Patrick Mar 31 '14 at 16:37
  • can you show your full uri ? I think the problem is `url + iKeywordIds` – cat916 Mar 31 '14 at 16:41
  • 1
    Your API function is probably expecting a URL that that looks something like .../api/controllername?ids=1&ids=2&ids=3. You need to convert your List on the client to the querystring that the function is expecting – Ian Gilroy Mar 31 '14 at 16:42
  • @minhcat_vo it's "api/ikeyword/getkeywordidsfromids/" – Patrick Mar 31 '14 at 16:44
  • @IanGilroy Hi thanks, and how can I do that? I tried "api/ikeyword/getkeywordidsfromids/iKeywordIds=1&iKeywordIds=2&iKeywordIds=3" but I still get the error: Exception:Thrown: "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'text/html'." – Patrick Mar 31 '14 at 16:44
  • 1
    You write some code that parses your list and generates the required querystring. For example, http://stackoverflow.com/questions/17096201/build-query-string-for-system-net-httpclient-get – Ian Gilroy Mar 31 '14 at 16:48
  • 1
    The parameter is called ids, not iKeywordIds. And I'm not convinced that the rest of the URL is correct either. Browse to the root of your site and use the built-in web api documentation to get the right URL. – Ian Gilroy Mar 31 '14 at 16:49
  • @IanGilroy I think I found one of the reasons, I had "/" at the end of the url and I should have "?" but I'm still not able to pass the List as it is – Patrick Mar 31 '14 at 17:02

1 Answers1

3

To call the Web API action with a list of ints in the query string, do it like this...

/api/ikeyword/getkeywordidsfromids?ids=1&ids=3&ids=4

or

/api/ikeyword/getkeywordidsfromids?ids[]=1&ids[]=3&ids[]=4

From your IIS logs, it looks like you need to properly format the list in your console app and append it to the URL. Your calling code should look something like this...

var parameters = ids.Select(id => string.Format("ids={0}", id)).ToArray();
var url = string.Format("/api/ikeyword/getkeywordidsfromids?{0}", string.Join("&", parameters));
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • Hi thanks, and how to I process the value in the WebApi method? – Patrick Mar 31 '14 at 17:00
  • `ids` should be a List of `int`s. You can pass it straight to `_iKeywordService.GetKeywordIdsFromIKeywordIds(ids)`... – Anthony Chu Mar 31 '14 at 17:07
  • It's working, please just correct your code so I can set your answer as correct: remove the "=" in the Format method: getkeywordidsfromids?{0} or else in the Api it will not receive the first value – Patrick Mar 31 '14 at 17:21
  • It's me who Thank you! – Patrick Mar 31 '14 at 17:24