2

I have Xamarin application that has POST request with array list of parameter and on my MVC WEB API we used code first Entity framework. Both was separated project solutions (.sln).

On my Xamarin project, I have PostAsync request which supplies List of array values.

  using (var client = new HttpClient())
        {
           Parameter =  string.Format("type={0}&param={1}",type, param[]);

            var data = JsonConvert.SerializeObject(parameters);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            using (var response = await client.PostAsync(url, content))
            {
                using (var responseContent = response.Content)
                {
                    result = await responseContent.ReadAsStringAsync();
                }
            }
      }

Then In my Web API controller I have same parameter with my client side also.

    [System.Web.Http.AcceptVerbs("GET", "POST")]
    [System.Web.Http.HttpPost]
    [Route("type={type}&param={param}")]
    public BasicResponse applog([FromUri] ProfilingType type , List<string> param)
    {
        if (ModelState.IsValid == false)
        {
            throw new ModelValidationException("Model state is invalid.");
        }

        try
        {
            if(type == ProfilingType.Login)
            {
                var command = new SendDataProfilingCommand(param);
                CommandHandler.Execute(command);
            }
            else
            {
                var command = new UpdateDataProfilingCommand(type,param);
                CommandHandler.Execute(command);
            }

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return new BasicResponse
        {
            Status = true,
            Message = Ok().ToString()
        };
    }

Since I'm not with the API, I want to test it first on Postman or even in the URL. but my problem was when i Try to test it using this url below

http://localhost:59828/api/users/applog?type=1&param=[1,Caloocan,Metro Manila,Philippines,0,0]

I received this message : No HTTP resource was found that matches the request URI ......

My Question is, How can I test my Web API with List Parameter on URL or in the Postman ? and What Format I can use when sending a post request into my Xamarin PostAsync request?

ralphralph
  • 41
  • 2
  • 7
  • 1
    Possible duplicate of [How to pass an array of integers to ASP.NET Web API?](https://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-asp-net-web-api) – Igor Oct 19 '17 at 18:54
  • See the duplicate link. You are building your URL incorrectly for an array. – Igor Oct 19 '17 at 18:55

1 Answers1

0

You don't need to send as Content.

    using (var client = new HttpClient())
    {
       Parameter =  string.Format("type={0}&param={1}",type, param[]);

       url = url + "?" + Parameter;

        using (var response = await client.PostAsync(url))
        {
            using (var responseContent = response.Content)
            {
                result = await responseContent.ReadAsStringAsync();
            }
        }
  }
Jesus Angulo
  • 2,646
  • 22
  • 28