So I am developing an API with Laravel 5.2 and I'm facing an important issue.
I have a UserController that will manage the users of my app.
This is my routes.php file:
Route::group(array('prefix' => 'api/v1'), function() {
Route::post('user', 'UserController@store');
});
And I have my UserController defined like that:
class UserController extends Controller {
public function index() {
return 'Hello, API';
}
public function create(){
}
public function store(Request $request) {
$user = new User;
$user->email = $request->email;
$user->password = $request->password;
$user->fbId = $request->fbId;
$user->ggId = $request->ggId;
$user->firstName = $request->firstName;
$user->lastName = $request->lastName;
$user->imageUrl = $request->imageUrl;
$user->country = $request->country;
$user->mobile = $request->mobile;
$user->gender = $request->gender;
$user->client = $request->client;
$user->save();
return Response::json(array(
'error' => false,
'userId' => $user->id),
200
);
}
public function update(Request $request, $id) {
}
}
And this is the output of php artisan route:list
+--------+--------+-------------+------+-------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------+-------------+------+-------------------------------------------+------------+
| | POST | api/v1/user | | App\Http\Controllers\UserController@store | web |
+--------+--------+-------------+------+-------------------------------------------+------------+
I'm using Postman to test my POST requests. Every time I make a POST request to /api/v1/user, I get a "405 Method Not Allowed" error.
Did I miss anything?
Is there anything I should do to fix this issue?
I have same problem with you which are I already set in my POST route for example "/api/v1/user",
and when I try to connect using POSTMAN (application to test API) , it return 405-Method Not Allowed,
and then i realize the url i was sent is using 'http' and after i change it to 'https' (with the 's' at the back)
then my API working like normal !
normally if we interact with different server , we must use 'https'
but if your application at the same server ,
it's okay to use 'http'
The real reason for my case is any interaction with any different server must use 'https' (This is the setup on my server)
Changing to https will cause it to work. At least, that is what I have experienced. Using POSTman with http always has this effect.
You need to separate your routes because all the users trying to get to your routes need a open session (logged in)
Try this
Route::group(array('prefix' => 'api/v1'), function() {
Route::post('/','UserController@store');
Route::get('/', 'UserController@index');
Route::group(array('before' => 'auth.basic'), function() {
Route::post('{user}', 'UserController@update');
});
});
Your authorized users routes should be in the second group
And your 405 Method not Allowed is $user->id
change it for $request->user()->id
I had the same issue on Laravel 5.8 when creating web-hooks
routes.
These routes use the web middleware and because of that, the VerifyCsrfToken route middleware group is also included. (Reference app/Http/Kernel.php
)
Because my POST request doesn't include a CSRF token we get this strange behaviour.
To fix this, I needed to add an exception to the VerifyCsrfToken middleware for the web-hooks routes. (Reference app/Http/Middleware/VerifyCsrfToken.php
)
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'web-hooks/*'
];
The above solution is for when the web middleware is used. However if you are creating API routes, it is better to use the api middleware instead because no CSRF verification is used in this middleware. (Reference app/Providers/RouteServiceProvider.php
)
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With