Question

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?

Was it helpful?

Solution

You can have only one parameter with [FromBody] attribute.

[HttpPost]
[Route("search")]
public HttpResponseMessage Search([FromBody] string term, 
                                 [FromBody] string category // this wouldn't work)
{
    // Code ommited for brevity.
}

Therefore WebApi expecting you to have key : =dogs. So if you would've called it with jQuery - you need to use something like this: $.post('api/test/search', { '': 'dogs' });

Edit:

Resources:

  1. Practical ASP.NET Web API
  2. Parameter Binding in ASP.NET Web API (MSDN)

OTHER TIPS

try using content tye as application/json instead of application/x-www-form-urlencoded and also remove formbody from post mehtod parameter

You need to make sure the value posted as no key and that it simply begins with =

So, instead of:

searchTerm=dogs

it should be just

=dogs

You can get all parameter value by using model binding like following create viewmodel class like following :

 public class SearchView 
    {
        public string SearchText { get; set; }               
    }

add above view model class in your web api like following :

[Route("api/Search"), HttpPost]
            public HttpResponseMessage Search(SearchView objSearchView)
            {

pass it in your js file like following :

var objSearchView = {};
objSearchView.SearchText  = "test";          

$.ajax({
    url: '/api/search',
    type: 'POST',
    contentType: 'multipart/form-data',
    data: data,               
    success: function (response) {
    alert(response);
    },
    error: function (e) {
    alert(e);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top