Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JoshP's avatar
Level 7

POSTing via form-body vs URI params

Hiya,

Quick question...

Is there a reason to choose one of these options over the other?

URI parameters

Route::post('recommend/{userId}/{type}/{typeId}', 'AlertsController@recommend'); And then just grab and validate the controller method's arguments.

Form-body

Route::post('recommend', AlertsController@recommend');

{
    "userId": "12345",
    "type": "some-type",
    "typeId": "23456"
}

And then use a FormRequest object in the controller to validate

This is for use in a non-public API. I realize both can work. Just curious if one may have advantages over the other.

0 likes
3 replies
deansatch's avatar

There is a limit on the length of a uri which varies from browser to browser - roughly about 2000 characters so if you had something like /recommend/{title}/{description} you might find your description is cut short. I learnt this the hard way with a GET request years ago but it should still be the same for POST

Also, I think even though you are using Route::post - those 3 params are GET so obviously no good for secure values. And you'd have to know them before loading the page/form unless you use JS to change the form action

Basically I would stick with posting to recommend as in your second example

1 like
sutherland's avatar

I'd stick with Route::post('recommend', AlertsController@recommend'); because I generally prefer fewer URL params, especially for post requests. If for some reason your needs change it gets more complex to manage your routes when a field/parameter becomes optional, new ones get added, or old ones get removed.

I think it only really makes sense to have more than one URL param for pages users will see in the address bar. A page users see like /racing/indycar/2017 is readable for users and should help with SEO, but if they're clicking a like button on a page they'll probably never see the URL, so there's no reason to muddy it up.

1 like
JoshP's avatar
JoshP
OP
Best Answer
Level 7

Thanks guys!

Since this is just for a closed api, the url-ness of the params didn't bother me at all. Likewise, there's no form involved.

I ended up going with option two. Not only does it seem more POST-native, but to be sure, having a route with three uri variables can get messy if you're not careful about the order the routes are declared. Three variables in a route is almost a catch-all!

Also, because it's an api, the simpler endpoint seemed like a better choice, and less likely to want to change.

Cheers!

Please or to participate in this conversation.