Skip to content
Advertisement

How to prevent Laravel API from processing parameters on query-string?

I would like to restrict my Laravel API from processing parameters as query-string when trying to authenticate the user. I’ve been trying with POSTMAN and all the time I’m able to get the token from my API whether I put the credentials on the body or as query-string in the url.

As per Laravel documentation I think that this is the behavior I want to avoid:

Retrieving Input Via Dynamic Properties

You may also access user input using dynamic properties on the IlluminateHttpRequest instance. For example, if one of your application’s forms contains a name field, you may access the value of the field like so:

$name = $request->name;

When using dynamic properties, Laravel will first look for the parameter’s value in the request payload. If it is not present, Laravel will search for the field in the route parameters.

I’m using Laravel 5.3 and PHP 7.1.0

Here is the POST by using query-string:

enter image description here

Here is the POST by using parameters in the body:

enter image description here

I have configured my CORS by using laravel-cors:

JavaScript

My routes (the relevant ones):

JavaScript

When listing my routes php artisan route:list I get:

JavaScript

My AuthenticationController:

JavaScript

My Kernel:

JavaScript

And I placed the respective configuration on config/app.php:

JavaScript

I don’t want to use dingoapi.

I checked these resources:

Last but not least, my composer.json:

JavaScript

UPDATE

Thanks to the answer given by “Basheer Ahmed” who pointed me in the right direction I ended up doing a Trait for parsing the body attributes that I want to get depending on the request:

JavaScript

This method it will be used mostly on create and update actions like follows, on AddressController:

JavaScript

In this way and by using $request->request->get('my_param'); I can be sure after testing how that method works, that I’m only getting the attributes of the body.

This is the test for AddressController on those methods:

JavaScript

Advertisement

Answer

Anything that is appended to the url bar is considered a get request and will be available through $_GET super global variable. I assume that laravel Request request will merge both post and get request and then when you try to call any paramter that is sent through get or post, You can get it through

JavaScript

But If you just try

JavaScript

You won’t get the similar result.

🙂

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement