Laravel API PUT/PATCH not sending parameters : Postman

Introduction

Laravel ships with a powerful, yet simple routing system. It gives developers a lot of flexibility when defining routes for their application. With a single line of command, you can create controller and route resources. You can use –resource or -r.

php artisan make:controller HelloController –resource

With the above command, we’ll get a controller that handles all HTTP requests for “hello”. Next, you can register the resourceful routes to the controller.

Route::resource(‘photos’, ‘PhotoController’);

Now if you see your routes, you can see :

Route list Laravel-Nepal

To view your route list, use command php artisan route:list in your CLI.

All good until here? Yes?

Now, we can focus on our main topic.

Getting Started

Let’s take a look at the route list. For hello.update route, we have PUT/PATCH method. When developing our APIs we test them using POSTMAN or any other app.

When testing route hello.update, you are most likely to get this problem of not receiving your parameters sent out from POSTMAN in case of PUT/PATCH method. The main question here is why do we get this error and how can we solve it?

Why don’t we receive the parameters?

According to HTML standard, you cannot send PUT/PATCH method. The only valid methods attributes are GET and POST.

If you send this way, this will be invalid and treated as <form>.

How do we solve it ?

Many frameworks simply use a POST parameter to tunnel the HTTP method including Laravel. To differentiate it as an update request, a hidden input with name “_method” and value “put” is included in the form.

This is a simple cheat many frameworks use.

If you’re following the article, you probably get the idea. If not let’s get into the solution. 🙂

Option 1 :

We can simply use post method and add _method formdata with PUT value.

Option 1 laravel-Nepal

Option 2 :

Use x-www-form-urlencoded to send parameters.

Option 2 laravel-nepal

Option 3:

Or simply change your route controller and use post method.

Route::post(‘hello/{hello}’, ‘HelloController@update’);


This is a common error which occurs during application development when using frameworks. You can easily solve this issue using any of the above methods.

Happy coding 🙂

 

Sharing is caring!

Related Post