I created a Search form to find posts on a MVC site.
Should the form type be POST or GET?
I know that being get is possible to bookmark the search and so on.
Any disadvantage of using GET?
Thank You, Miguel
I created a Search form to find posts on a MVC site.
Should the form type be POST or GET?
I know that being get is possible to bookmark the search and so on.
Any disadvantage of using GET?
Thank You, Miguel
Even though GET
is the more conventional solution for querying data, there are plenty of cases where a GET
is too limited to perform a /search
.
The essence of the problem, is that a GET
has no request body, and for that reason it cannot handle more complex request. In essence a GET
can only send data using the URL. A URL can contain path parameters and query parameters, which are just a set of key-value pairs where both key and value are of a string
type.
By contrast, a POST
can in addition also carry an entire JSON document in its body. By restricting ourselves to a GET
, we cannot use any of those JSON features, and as a result we cannot send proper objects or arrays to our back-end.
How valuable are semantics, really ? Should we follow a workaround, which could result in technical debt, just to honor a naming convention?
As mentioned above, one of the possible problems, is that it cannot deal with arrays.
For instance, in a webshop, you want users to present with a catalog of products presented in a list. You want to offer your users the ability to select a number of vendors using checkboxes to filter the list. It would result in an array such as selectedVendors = ["vendorA", "vendorB"]
.
If we want to respect the convention of using a GET
, then we have to find an acceptable workaround which allows us to send a list of vendors to the back-end without using a request body.
With only query parameters at your disposal, it is possible to model it as a bunch of booleans: includeVendorA
, includeVendorB
, ...
Unfortunately, that's hard to maintain and difficult to document.
The front-end could actually perform multiple queries. i.e.
/search?vendor=VendorA
),/search?vendor=VendorB
)First of all, it has a performance penalty, because it needs multiple round-trips. But secondly, it also breaks the ability to support paging.
Add incremental indexes to your query parameter names. (e.g. /search?vendor1=VendorA&vendor2=VendorB
)
Again, hard to document, and not supported by OpenAPI neither.
If we can just accept that a POST
is more suitable for a /search
, then we don't need any of those workarounds.
Specifically for sending arrays over GET
, there still isn't a standard yet. However, slowly frameworks and languages are shifting towards a de facto standard: /search?vendor[]=VendorA&vendor[]=VendorB
.
One more specific shortcoming of the GET
, would be the ability to model conditional filters: (e.g. in a ticket system: "give me all tickets which person X created and all tickets which person X closed", or in a webstore: "give me all products that are in discount and all products with free shipping"). They translate to database queries with a mix of AND
and OR
statements.
With GET
verb, the arguments will be on the URL
, there is not http request body for GET
. Using POST
may not be the appropriated solution for this, which in case we also have the arguments on the URL but we can send more data into the request body in a specific format (json, xml, text, etc). Using GET
as the verb name said, is the best solution for it and you also get the benefit of copy/paste this url and share anywhere. There is no limit for arguments on the URL of http.
The HTTP protocol does not place any a priori limit on the length of a URI. Servers MUST be able to handle the URI of any resource they serve, and SHOULD be able to handle URIs of unbounded length if they provide GET-based forms that could generate such URIs. A server SHOULD return 414 (Request-URI Too Long) status if a URI is longer than the server can handle (see section 10.4.15).
but as the Jasen comment, browsers can have limit.
Anyway, remember to use method do avoid sql injection
. If you are going with ado.net
, use Parameters
. If you are using and ORM
it will care for you.
Unless you are modifying data, I would go with a GET. I see no drawbacks if you are always getting data and not manipulating data.
Search is not necessarily a Post, you're trying to get some data, not modify or insert.