How to fix 'The GET method is not supported for this route. Supported methods: POST'
Asked Answered
C

7

5

I'm developing a REST service with Laravel to consume it from a mobile app. It works properly on local, but not on hosting. After several tries, I developed a basic example to test the POST method, but it returns the same error.

api.php file

Route::post('/test', 'testController@test') ;

testController.php file

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class testController extends Controller
{
    //
    public function test(Request $request)
    {
        return response()->json(['mensaje' => 'POST access successful']); 
    }
}

POST request is always returning the same error, and I am using POST on petition: 405 Method Not Allowed. The GET method is not supported for this route. Supported methods: POST.

POSTMAN request

I have investigated this topic and I have read it could be due CORS. So, I have installed spatie/laravel-cors with its default config, but POSTMAN is still showing the same error. Some help, please?

SOLVED: Thanks all! Definitely, it was not a CORS problem. My hosting server makes a redirect by default, losing POST parameters in the way.

Chili answered 12/7, 2019 at 12:4 Comment(4)
is your domain https://midominio.com/ actually working ?Shammer
It's not exactly this domain, but yes it's working. Furthermore, It has been tested with GET method and work properly.Chili
so what is you url which works ?Shammer
My hosting server makes a redirect by default, losing POST parameters in the way. SO how did you solve it if you remmber?Workwoman
N
11

If you have come across this error while using POSTMAN, you need to go into the "Settings" part of your route's parameters and disable 'Automatically follow redirects'.

automatically follow redirects, off

Noelyn answered 23/7, 2020 at 8:31 Comment(2)
It worked when I enabled instead of disable. But thank you for you help!Tubercle
I ran into this issue again and once the redirects was disabled I got a 301 error -- using valet secure and making sure my local SSL was enabled overcame the issue.Noelyn
L
10

Sometimes the problem arises when you have trailing froward slash.

For example:

https://somedomain.com/api/test/

Simply remove the trailing forward slash so the url will be

https://somedomain.com/api/test
Lasko answered 18/8, 2021 at 8:33 Comment(0)
L
5

It's sometimes because you're using 'http' on an 'https'-based endpoint. Try replacing the http with https.

Looker answered 9/6, 2022 at 13:29 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ebby
If your server force to use https it best to replace the http with https. Thank you for the answerAgog
C
4

If you use Postman the problem can be that you forgot to add the headers on your request : Accept -> application/json

enter image description here

Cicero answered 23/4, 2023 at 20:19 Comment(0)
S
3

This is because of CORS (Cross Origin Resource Sharing) is protected and you are not allow to call your api from other origin. To allow put below header setting to your routes in api.php

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization'); 
Shammer answered 12/7, 2019 at 12:12 Comment(0)
R
0

Are you sure you call /api/test because in api.php route file there is a prefix on routes

Redwood answered 12/7, 2019 at 18:5 Comment(0)
O
0

check if you're handling JSON data then your API request must have a header for Accept: application/json

header('Accept:  application/json');
Ovum answered 13/6 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.