Laravel doesn't support DELETE & PUT requests? - php

Building my API it turned out, that if I am using DELETE or PUT requests Laravel throws 405 Error (Method Not Allowed). How to make it work?
Laravel error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.

Laravel does support DELETE and PUT. You defined your route wrong, it only allows GET-Requests.
Route::put('/put', 'Controller#function');
Route::delete('/delete', 'Controller#function');
See docs here

Related

Laravel Fortify and JSON based registering result in CRSF mismatch

I'm currently toying with Laravel 9.x and Fortify.
For the starter here my environnement :
Laravel 9.19
Fortify 1.14
Postgre 15
I try to achieve something I thought was possible from reading the Fortify doc, using a third-party UI (e.g.: Mobile App) to register and login user.
So, following the documentation guide I deactivated the views generation, and migrated the tables and launched my test server using php artisan serve.
Then I try using postman to post the following json to the /register route provided by Fortify.
Postman has been setup with the following headers:
Content-Type: application/json
Accept: application/json
{
"name": "test1",
"email": "test1#example.com",
"password": "MyPassw0rd!",
"password_confirmation": "MyPassw0rd!"
}
The response returned by the request was an error 419 CSRF Token mismatch, which I understand since Laravel enforce the use of CSRF token.
Therefor I tried to add the /register route to the except array inside the middleware VerifyCsrfToken and tried again and this time I got a 201 created response.
From my understanding since the /register route exists within the web guard hence the CSRF token mechanic.
Since my final goal is to use Fortify with third-party frontend, how can achieve that without putting the route inside the except array (if possible)?
Is there a parameter to change inside config/fortify.php to allow this behavior?
Thanks for reading.
After playing I found the solution inside the middleware section of config/fortify.php
Replacing
middleware => ['web'],
with
middleware => ['api'],
Allow to user the register route without having to deactivate the CSRF on the route .

All requests ends up in GET route. Laravel

I have a Laravel(8.83) application. API routes end up in GET requests always.
To check the problem I have deleted all the routes except one. Now I have just one route:
Route::put("cart/{id}", function(Request $request, $id) {
return $request->all();
});
Although I make PUT request on the postman, the result is: "The GET method is not supported for this route. Supported methods: PUT."
I have tried PUT, PATCH, POST, DELETE, and OPTIONS. All ended up in the same error.
Edit: 3 Oct 2022
I run php artisan optimize:clear and composer dump-autoload several times. None resolved the issue.
The project is in production, deployed on a server. I have made a request from a Vue.JS web app client, and the result is the same. I don't think the problem is caused by Postman.
The request method is probably getting modified in Postman or in webserver config, or in .htaccess.
Here's one thread on a similar issue in Postman - Postman support
For a reference to a working .htaccess - Laravel .htaccess GitHub

HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST in Laravel

I have used REST API & Web in same laravel project. Both working in localhost. But not in server.
I have used Postman for the REST API checking. I'm sharing my route code below,
This my api.php code.
// Customer registration
Route::post('signup/', "api\CustomerAccountController#customerRegistration");
REST API testing from the server. Go to the link below.
https://drive.google.com/file/d/13N71hqWQFXZcKNzSjr1eqqe7DUhPnnr6/view?usp=sharing
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file /home1/foresqtn/quizapi.biconconsultants.com/quizapi/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php on line 117
This code is uploaded to the server.
Now I am sharing the localhost response with same code. Go to the below link.
https://drive.google.com/file/d/1gK1cO7tmvG3f6EsYqQS-1n3OpXCrVskr/view?usp=sharing
Now I couldn't find what I'm doing wrong here. I am running same code.
Your valuable response will be appreciated.
Problem Solved
In Postman when I'm trying to send a request using the endpoint, in the end of the URL I'm adding '/' that's why I was getting the above Error. Now the error is resolved.
If you just use "http" instead of "https" your problem will be solved. It worked for me.
If you're route in the auth middleware group, just try to add Bearer token, everything will work

Laravel 5.4 Error 405 Method Not Allowed Error

I have Laravel 5.4 and VueJs application. When I run it in Localhost it was worked but now. I host in a server it gives the error GET > 405 Method Not Allowed Error. But GET requests are work fine. But this function I'm not using GET request. I using POST. But I giving this error. I have Installed CORS also. What can I do?
This is my POST Request in VueJS
this.$http.post("api/sendbooking",this.booking)
.then(function (response){
console.log(response)
})
This is api.php
Route::post('/sendbooking',[
'uses' => 'BookingController#setBooking'
]);
You're attempting to make a request that is unauthorized or otherwise not configured correctly. 405 means unauthorized HTTP verb in the request. Double check your route files that you can POST to the route you want to reach.
Anytime I see this is when I accidentally try to use GET on a POST route or similar.
And since you mentioned CORS, make sure all the required verbs are listed as allowed.

Symfony2 app.php receiving POST request as GET app_dev.php works fine

So I'm creating a route that requires posting. The route is as follows:
my_route:
path: /myroute/login
defaults: { _controller: "MyBundle:Default:login"}
methods: [POST]
So I use postman to hit app_dev.php/myroute/login and I get the correct response that I am looking for, currently the page just returns "hello world". Now when I change postman to just hit app.php/myroute/login I get an error saying:
The server returned a "405 Method Not Allowed".
I'm really confused as to why it says method not allowed so I tailed the prod.log file and got the following:
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: "No route found for "GET /myroute/login": Method Not Allowed (Allow: POST)"
I'm sending the requests in postman as POST requests but somehow symfony is only seeing them as GET requests. Again I can change the url to app_dev.php/myroute/login and hit send and it works. I thought it was a caching issue so I tried the following to clear the caches:
$ php app/console cache:clear --env=prod --no-debug
This still doesn't clear my problem so I even removed all the files from app/cache/prod as well. I can't find anyone having issues that are similar to this so I'm hoping someone can point me in the right direction. I also thought it might be
After some more digging I discovered what the issue was. So I thought that maybe it was postman that was sending the incorrect method so I googled around for that which lead to this
Postman sending POST as GET
which lead to a question about htaccess files:
Redirect POST htaccess
So first off I set postman to hit app.php/myroute/login and xdebug showed the request method as GET. I moved the .htaccess file and hit the same url and the request method showed up as POST. Instead of hitting that url I moved my .htaccess file back and pointed at just /myroute/login and everything lit up. This was an extremely annoying exercise so I hope that this will save someone else in the future looking for the same problem.
for me the case was my mistake, i.e. i was sending to http instead of https

Categories