Search code examples
laravellaravel-passportlaravel-request

NotFoundHttpException with my Request file on api route


I made a route in the api file that allow everybody to create users :

Route::post('users', 'UserController@addUser');

When I called it in postman without using request validation it works. But when I created my request file and use it, Laravel return a NotFoundHttpException.

Here's my request file :

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserAddRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_id' => 'required|numeric',
            'name' => 'required|string',
            'email' => 'required|string',
            'password' => 'required|string'
        ];
    }
}
public function addUser(UserAddRequest $request){

    $user = new User;
    $user->instance_user_id = $request->input('user_id');
    $user->name = $request->input('name');
    $user->email = $request->input('email');
    $user->password = $request->input('password');
    $user->save();

}

There is no form because it needs to be send directly to the server with post method. I declared my route in routes/api.php and I call it with the url /api/users The api in that case doesn't need to check credentials.


Solution

  • I solved my problem :

    The NotFoundHttpException was raised because I didn't send my parametters correctly and Laravel doesn't where to redirect me back. When I send directly the request to the server there is no url declared for where I'm coming.