I'm trying to call an endpoint using Microsoft's Web API. I'm running into an issue when trying to POST a simple string value. Here is the API controller method.
[HttpPost]
[Route("search")]
public HttpResponseMessage Search([FromBody] string searchTerm)
{
// Code ommited for brevity.
}
The problem is that the searchTerm variable comes across as null. Here is the raw view of the POST from Fiddler:
POST http://localhost:51889/search HTTP/1.1
Host: http://localhost:51889
Connection: keep-alive
Content-Length: 15
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
Postman-Token: 4fc51a0c-48ca-a1cd-3df8-70155c3cb5fc
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
searchTerm=dogs
I'm sending the request using Postman. This StackOverflow user had a similar problem and one of the answers was to read Rick Strahl's blog post on the topic. Rick mentions that I need to include the [FromBody] attribute, which I did include. However, this doesn't fix the issue. The only thing I can do to get this to work is to create a new class like so
[HttpPost]
[Route("search")]
public HttpResponseMessage Search([FromBody] Search term)
{
// Code ommited for brevity.
}
public class Search
{
public string Term { get; set; }
}
I would rather not have to create new classes for each POST with simple types like this. Am I missing something? What do I need to change to get simple types to work with my posts without having to create new classes?
[FormBody]and pass the search term on the url:search?searchTerm=mySearch- Eben Roux