1

I'm trying to validate the request in my API but when I use the validate method or even create a class that extends FormRequest, it gives me the 405 error. Below you can see my controller and the way routes are defined. It only gives me 405 error when the validation fails, I assume, maybe, somehow there is a callback function sending GET method request but I have no clue how to fix it.

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Interfaces\PostRepositoryInterface;

class PostController extends Controller
{

    private $postRepository;

    public function __construct(PostRepositoryInterface $postRepository)
    {
        $this->postRepository = $postRepository;
    }


    public function index()
    {
        return $this->postRepository->all();
    }

    public function store()
    {

        request()->validate([
            'title'=>'required|min:2',
        ]); 

        return $this->postRepository->createPost();
    }

    public function show($postId)
    {
        return $this->postRepository->findById($postId);
    }

}

Error message:

Route::group(['middleware' => 'auth:api', 'namespace'=> 'API'], function(){


    Route::get('post'            , 'PostController@index'  );
    Route::post('post'           , 'PostController@store'  );
    Route::get('post/{post}'     , 'PostController@show'   );
    Route::get('post/{post}/edit', 'PostController@edit'   );
    Route::post('post/{post}'    , 'PostController@update' );
    Route::post('post/{post}'    , 'PostController@destroy');

});
Rwd
  • 34,180
  • 6
  • 64
  • 78
Newtz
  • 43
  • 7
  • Please can you show the form that is making the request? Also, if you're using an javascript with it please can you show that too. – Rwd Dec 28 '19 at 22:04
  • I'm using Insomnia software to simulate a request. – Newtz Dec 28 '19 at 22:06
  • ... the point of the comment was we cannot see what request you are using. Until you've shown us otherwise, the obvious answer that we will assume is that you are doing exactly what the error says - sending a request for which you have no route configured, either wrong method or wrong URL. So, once again - either show us the code that makes the request, or describe (*exactly*) the request you are creating with whatever Insomnia is. Eg the screenshot seems to show it was generated on your home page, at `/` - there is no route shown for that in your code. – Don't Panic Dec 29 '19 at 10:43

1 Answers1

1

Thanks everybody that tried to help, i found the solution in this post

MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5

Newtz
  • 43
  • 7