Home > Software engineering >  Error using FormRequest in Laravel 9 (The POST method is not supported for this route.)
Error using FormRequest in Laravel 9 (The POST method is not supported for this route.)

Time:08-12

I'm creating an API in Laravel 9, and I'm getting this error when using my FormRequest.

Edit: I already tried with: php artisan route: clear and also php artisan route: cache

php artisan route:list

  POST       _ignition/execute-solution ...... ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController
  GET|HEAD   _ignition/health-check .................. ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController
  POST       _ignition/update-config ............... ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController
  GET|HEAD   api/categories ......................................... generated::lHkZOL5tf3LIcEQ8 › CategoryController@index
  POST       api/sections ............................................ generated::UOlJ66qvitxlxVFJ › SectionController@store
  GET|HEAD   api/statuses ............................................. generated::QEy3vMPtQoyTF9ZF › StatusController@index
  POST       api/surveys .............................................. generated::ugYzFBQ10OqCe15t › SurveyController@store
  GET|HEAD   api/surveys .............................................. generated::cp4J8q5S2iAURPD9 › SurveyController@index
  GET|HEAD   api/user .......................................................................... generated::ZAKvw5kCNfZA65gG
  GET|HEAD   sanctum/csrf-cookie ......................... sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show

My Route

Route::post('/sections', [SectionController::class, 'store']);

My Controller

class SectionController extends Controller
{
    public function store(StoreSectionRequest $request)
    {
        return $request;
    }
}

My problem is that when I use StoreSectionRequest it throws me the error:

The POST method is not supported for this route. Supported methods: GET, HEAD.

But when I just use Request it works normally.

class SectionController extends Controller
{
    public function store(Request $request)
    {
        return $request;
    }
}

This is how it works correctly.

I don't understand what happens, because when creating my other routes I didn't have this problem.

class StoreSectionRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required | string ',
            'description' => 'nullable | string',
            'survey_id' => 'required | exists:surveys,id'
        ];
    }
}

CodePudding user response:

Run php artisan route:list on console. You will get all routes of your appilcation. This is list of Route::post('/sections', [SectionController::class, 'store']); routes.By route list you will get idea of URI and Action

CodePudding user response:

When you include the StoreSectionRequest, you are also including validation.

A validation error is beng thrown, but you have probably not indicated to Laravel that you need a json response and not a redirection to GET at the same route.

Make sure your API request includes Accept: application/json in the headers so that Laravel knows its a json request

  • Related