I am working on MVC 4 Web Api with Jquery and Jqgrid, till now I was posting multiple data to my post controller action.
My action looked like the one below…
[ActionName("FetchProducts")]
public List<ABC> PostProducts(Product model)
{
return _service.GetSomething(model);
}
public class Product{
public string Name {get;set;}
public string Category {get;set;}
//.... and alteast 5 more properties
}
and my jquery call was something like the one below…
$.ajax({
type: "POST",
url: /api/FetchProduct,
data: this.getData(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: callback
});
function getData(){
return JSON.stringify({
Name: "from somewhere" ,
Category: "from somewhere",
Price: "from somewhere",
ABC: "from somewhere",
XYZ: "from somewhere",
//... and many more....
});
}
and this works ! but my friend at work says
I am actually only getting data and should use ‘GET’ and not ‘POST’. As GET is used to retrieve remote data, and POST is used to insert/update remote data.
and I also feel he is right. So how should I do this with ‘GET’ ?
do I have to pass all these parameters (there are atleast 10 of them) as query string ?
like for eg : api/FetchProduct/?Name='aaa'&&Category='vvv'&&.........
So my question is what should one do in such a scenario ? I would like to know what other devs think about this. Thanks
As you query string grows, you will run into problems with the maximum length of the URL, which is browser dependent.
If you have a lot of parameters to pass, a POST is perfectly acceptable even if you are really only GETting data. What you will lose, however, is the ability for the user to bookmark a particular page with the query string.
One case where you should absolutely never use GET is if any of the parameters you are passing are sensitive.