How can i send blank Parameter in a URL in a RESTAPI


#1

How can i send blank Parameter in a URL in a RESTAPI

I have a route like this one below
$app->post(’/customer/find/{id}/{name}/{dba}’, function (Request $request, Response $response, array $args)
It is called to search for customers in the DB, if i provide values for all 3 it works fine, but if i for example dont have a value for id because user does know it what would i send ? Specially how do i deal with this for dba as its the last param

I tried customer/find/2/w/ed or customer/find//w/ed or customer/find/%/w/ed but all return errors like Bad Request or Page not found.


#2

What if the user just send a “?” as id, and you filter that one out?


#3

No you can’t… Because web server will read customer/find//w/ed => customer/find/w/ed.

The solution is you have to create query param in the url.

Other solution is you can send through body request.


#4

Yes came to same conclusion , so I only use it for simple get stuff like if I need customer info and query by customer I’d, for everything else I start to use post and use a json in body which allows me to send blank values. Then on my stored procedure I use concatenation and add ‘%’ to the param send by user to query. Would have been nice if the would have been a way but once you get used to convert the body in to a array it’s pretty simple and only takes a few extra lines of code.


#5

I just read the user guide and found this:

$app = new \Slim\App();
$app->any('/books/[{id}]', function ($request, $response, $args) {
    // Apply changes to books or book identified by $args['id'] if specified.
    // To check which method is used: $request->getMethod();
});

Maybe using brackets on param will solve your issue.
https://www.slimframework.com/docs/v3/objects/router.html


#6

I looked at this as well but there seems to be some changes from Slim 2 to Slim 3. In slim 2 there is the option to specify a default value for optional param
Slim 2 Docs

$app = new Slim();
$app->get('/archive(/:year(/:month(/:day)))', function ($year = 2010, $month = 12, $day = 05) {
echo sprintf('%s-%s-%s', $year, $month, $day);

Slim 3 Docs
$app->get(’/users[/{id}]’, function ($request, $response, $args) {
// responds to both /users and /users/123
// but not to /users/
});

Is there still an option to specify a default value for the optional param ?