we have a RESTful API that includes an endpoint for search
apiserver/v5/search?q=[search text]
for any query, it passes it off to solr and returns the result like
{code:200,results:[{..},{...}]}
if q parameter is omitted, it returns:
{code:412,message:"parameter q is required"}
if q parameter is EMPTY, eg ?q=
then it also returns 412:
if q parameter is omitted, it returns:
{code:412,message:"parameter q is empty"}
The design is the the search PAGE with eg example.com/search?q=
will show a default view and not make the query if it is empty, but instead just show a search box or default information.
My question is, Is it poor design or very non-standard to have the API return error on empty query? ie, does it make the API very unexpected/uncomfortable, or is it quite reasonable, to enforce that "you won't get results so you should be handling this in front end behavior"
Below is the relevant code.
NOTE: search function handles all the validation and sanitation and other stuff and is outside the scope of the relevant code and question
<?php
require('search.php');
if(!isset($_GET['q'])
echo '{"code":412,"message":"parameter q is required"}';
// should we do this to enforce they should handle front end?
if(empty($_GET['q']))
echo '{"code":412,"message":"parameter q is empty"}';
echo search($_GET['q']);
isset
check AND theempty
check. Theempty
check will do the work ofisset
already. I see that your error message is slightly different for isset and empty, and if that's a need of yours for some reason then stick with it, but if it's not then I would recommend to stick withempty
\$\endgroup\$