1

I am attempting to send a string to my web api like this:

public IList<Product> GetProducts(string myString)
{
    IList<Product> retVal = null;
    using (var client = GetClient())
    {
        HttpContent httpContent = new StringContent(myString, Encoding.UTF8, "application/json");

        // HTTP POST
        HttpResponseMessage response = client.PostAsync("api/products", httpContent).Result;
    }
    return retVal;
}

In my ProductsController my action method looks like this:

// POST: api/products/filter
[HttpPost("filter")]
public IList<Product> Filter(string filter)
{

}

For some reason the filter param keeps coming in as null. Any ideas why?

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231

2 Answers2

2

You should decorate parameter with [FromBody] attribute as post will not take value from url I suspect

VidasV
  • 4,335
  • 1
  • 28
  • 50
0

Looks like youre posting to api/products, not api/products/filter. Signature for the method should be as Vidas says with the from body attribute, but posted to the correct url.

[HttpPost]
[Route("api/products/filter")
Public ilist<product>  Filter([FromBody] string filter)
{...}

While the route isnt specifically necessary, it can help you specify the url you are working with!

Chris Watts
  • 822
  • 1
  • 9
  • 27