5

I'm having an odd issue with a POST form in Laravel.

When sending a post request, my Laravel throws an MethodNotAllowedHttpException. Upon looking into the errormessage, I can see that Laravel thinks that my request is a GET request, which it is not.

When looking at both POST data and GET data of the errorpage, Laravel seems to think that they are both empty. This leaves me a bit confused, since it seems that some kind of redirect is going on, the HTTP_REFERER on the error is the page I'm posting from.

I've had this issue before, where making a named route solved my problem, but I'm making a simple CMS, so templates for a form is used, and it's not possible for me to use named routes, without allowing the user to use Blade syntax which is a bad idea.

My route is as follows (Simplified to a "Hello world"):

Route::post('/signup/add', function(){
    echo "Hello world"; 
});

http://pastebin.com/EsAeyHFx <- Full routes.php

http://pastebin.com/ByHdUFcK <- My form. Nothing fancy, only plain text/radiobuttons input. No html or anything special.

The even more strange part on this, is that I have another form (login form) that does not result in this behavior.

I have been looking at several other questions on StackOverflow, but they all seem to end up being a mistake of sending POST data to a GET route. This is not my case.

If I change the route from POST to GET, it works fine.

I've also tried to change the action of the form to GET and use the hidden field _method and set it post - No success.

Can someone tell me what is going on with this Exception and how to fix it?

ADDED: After some experimenting, I found out that when using 3rd party software (Like chrome extension Postman) and sending a POST request to the page, it works as inteded.

Fizk
  • 1,015
  • 1
  • 7
  • 19
  • Try URL::to('/signup/add/') in the "action" attribute of the form instead of just "/signup/add". Depending upon folder structure of your application it might change. – Sameer Mar 29 '15 at 08:49
  • The form is pure html, so the action should not be able to change. When inspecting the form, the action is still "/signup/add". And the form is a part of a template, so I can only use html, not Laravel or Blade (might not be entirely clear in the question) – Fizk Mar 29 '15 at 09:12
  • Could you post the entire html source code of the form page? I think the problem is in this page. I have tried to build a simple form by using your code and it worked. – mininoz Mar 29 '15 at 09:20
  • Try doing a normal POST request to the URL from any REST client like [this](https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en) and if it working the problem is with HTML page. – Sameer Mar 29 '15 at 09:25
  • Thanks for your time - I have found the problem :) Making an answer now! – Fizk Mar 29 '15 at 09:27

2 Answers2

11

Ok, after some digging around, making forms in jsfiddle, using jQuery and so on, I found the problem!

My form has a trailing slash in the action attribute, which it supposedly is not allowed to.

The solutions was simply to change my code to action="/signup/add" instead of action="/signup/add/

Man, I feel stupid...

Fizk
  • 1,015
  • 1
  • 7
  • 19
0

The MethodNotAllowedHttpException error means that the verb of the query is not consistent with the expected routes.

Do a php artisan route: list for listing routes and look in development tools

ushi-deshi
  • 31
  • 4