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?

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm I tried it with FromBody and still null. Should content-type be application/json if sending a string of json?
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!

2 Comments

Sorry I apolgize. At the top of the products controller I have api/controllerName specified.
My observation was based on your getproducts method, but looks like you got it sorted in the end which is the main thing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.