Reputation: 16512
I have an Asp.Net Web API
with the following function
[HttpPost]
[Route("Employees")]
public IHttpActionResult Employees(SearchBindingModel searchOptions)
{
...
}
It's a function to search employees, so it should be a HttpGet
instead of HttpPost
. The problem is that the SearchBindingModel
is really complex and I don't know how I could write it as a query string.
The class look like this
public class SearchBindingModel
{
public IList<Criteria> Criterias { get; set; }
}
public class Criteria
{
...
}
public class FooCriteria : Criteria
{
...
}
public class BarCriteria : Criteria
{
...
}
Since a query string cannot contain hierarchy should I rethink my Web API?
or should I keep using HttpPost
instead?
Upvotes: 3
Views: 855
Reputation: 2230
In short, follow best practise where it is possible but don't get hung up on it. It seems like you have a valid use case for using a POST. Being 'truly RESTful' is mostly down to interpretation of several competing standard (http client and server which are ever so slightly different etc).
For complex large models it is sometimes just not practical to use a GET, depending on the consumer of your API (via a browser or not) you may want to take into considerations browser URL length limits for the query string as described over https://stackoverflow.com/a/417184/1422377 This is particularly important if you will be passing in lists of a complex model.
Alternatively, if you really must use GET, WEB API will accept the model from the Body like the sample below. However, I would advise caution with this as certain clients will not be able to send a body to a get request (for example if your client was .net they would have to use the RestSharp lib instead of the native libraries).
public IHttpActionResult Employees([FromBody]SearchBindingModel searchOptions)
Upvotes: 2