2
votes

I've defined the following route in Laravel:

Route::group(['prefix' => 'api'], function() {
    Route::post('login', [
        'uses' => 'Auth\AuthController@login',
        'as' => 'auth.login',
    ]);
});

And I'm using Postman to send a request like this (you can also see the results):

enter image description here

Why am I getting a MethodNotAllowed exception???? I also tried creating a form in an empty html file, with the method set to post. but got the same results.

EDIT

If i add a route::get that shows a login form, after the post request in Postman it shows that login form.

enter image description here

EDIT 2:

output of php artisan route:list for our route entries:

+--------+----------+--------------+---------------------+----------------------------------------------------+------------+
| Domain | Method   | URI          | Name                | Action                                             | Middleware |
+--------+----------+--------------+---------------------+----------------------------------------------------+------------+
|        | GET|HEAD | /            | guest.home          | App\Http\Controllers\GuestController@index         |            |
|        | GET|HEAD | a/dashboard  | admin.dashboard     | Closure                                            |            |
|        | POST     | api/login    | auth.login          | App\Http\Controllers\Auth\AuthController@login     |            |
|        | GET|HEAD | api/login    | auth.login          | Closure                                            |            |
|        | GET|HEAD | api/logout   | auth.logout         | App\Http\Controllers\Auth\AuthController@getLogout | jwt.auth   |
|        | POST     | api/register | auth.register       | App\Http\Controllers\Auth\AuthController@register  | jwt.auth   |
|        | GET|HEAD | m/dashboard  | moderator.dashboard | Closure                                            |            |
|        | GET|HEAD | pu/dashboard | premium.dashboard   | Closure                                            |            |
|        | GET|HEAD | u/dashboard  | user.dashboard      | Closure                                            |            |
+--------+----------+--------------+---------------------+----------------------------------------------------+------------+

EDIT3

One more curious thing. If i set the method to Route::any, I get rid of the exception, but then I can't access the post parameters. i.e. I don't have any post parameters.

EDIT 4:

If I add a route::get and show the login view there and send the login credential, it works. But not in Postman.

2
Did you try php artisan route:clear?xAoc
Nope. Let me try that.Milad.Nozari
Yup, i tried it and it didn't work. I added a Route::get to the routes file, in which I show a view with a login form. And now when I send the post request in postman i get that login form which is bound to the get route.Milad.Nozari
Very weird. Try this command php artisan route:list You will see all registerde routes. Is POST /api/login there?xAoc
Yeah, I've tried that, and it shows exactly what I expect to see. Dude weird doesn't even begin to explain this :(((((( It's not like this is the first time I'm defining and using routes in Laravel.Milad.Nozari

2 Answers

1
votes

Use x-www-form-urlencoded instead of form-data in postman, See the difference below.

enter image description here

form-data

multipart/form-data is the default encoding a web form uses to transfer data. This simulates filling a form on a website, and submitting it. The form-data editor lets you set key/value pairs (using the key-value editor) for your data. You can attach files to a key as well. Do note that due to restrictions of the HTML5 spec, files are not stored in history or collections. You would have to select the file again at the time of sending a request.

enter image description here

urlencoded

This encoding is the same as the one used in URL parameters. You just need to enter key/value pairs and Postman will encode the keys and values properly. Note that you can not upload files through this encoding mode. There might be some confusion between form-data and urlencoded so make sure to check with your API first.

0
votes

Unfortunately the problem was with Postman3. I'm using Advanced REST Client now, and it works alright. Postman would send GET requests no matter what method I chose.