Delete and update do not work in apijson error MethodNotAllowedHttpException

-1

I am developing an api json. I'm finding that I miss the following error:

MethodNotAllowedHttpException

I have executed the php artisan route:list and the routes are well defined. The problem I find that the update and the delete do not work correctly.

The route is defined as web.php :

Route::resource("perros","PerrosController");

Requests with postman, I am sending them with id in the case of delete and with data and id in the case of update .

I have commented the csrfTocken :

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //\App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];
    
asked by CodeNoob 01.04.2018 в 14:27
source

2 answers

0

Bearing in mind that browsers can not normally send PUT, PATCH or DELETE, Laravel then waits for POST to be sent with a _method field in which it contains the desired verb (PUT, PATCH, DELETE).

If it were a form, it would be something like this:

<input type="hidden" value="DELETE" name="_method"  />
    
answered by 01.04.2018 в 15:30
-1

The solution was so easy that it was complicated

I'm doing an api json, where postman always sends me a MethodNotAllowedHttpException '.

Therefore it did not work for me:

<input type="hidden" name="_method" value="DELETE" />

That is fine for the case of making a form in html.

First: Define the route correctly

The correct routes must be defined correctly in routes/api.php :

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::resource('perros', 'PerrosController');

Second: Make the right questions

In case of UPDATE

PUT /api/perros/28?nombre=juanmaria HTTP/1.1
Host: apijson.local
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 81a331e8-2b9a-4b85-8c70-4000800dae9c
------WebKitFormBoundary7MA4YWxkTrZu0gW--

I was writing:

http://apijson.local/api/perros?id=28&nombre=juanmaria

Where it has to be

http://apijson.local/api/clientes/28?nombre=juanmaria

In case of DELETE

DELETE /api/clientes/28 HTTP/1.1
Host: apijson.local
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: f9b70db9-a50b-48c6-adea-9069ca4903f0
------WebKitFormBoundary7MA4YWxkTrZu0gW--

I was writing

http://apijson.local/api/clientes?id=28

And I had to write

http://apijson.local/api/clientes/28
    
answered by 01.04.2018 в 16:34