Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request not allowed exception in lumen framework

I have started making some routes following the lumen documentation, where it shows basic GET and POST routing.

So I have tried to make some tests in order to understand how they work. While the GET method seems working as expected, the the POST router seems encounter some problems. Here below my test router:

$router->post('/foo', function ($req) {
    var_dump($req); die();
});

Then I have tried to make a POST request using postman as below:

url : http://localhost:8000/foo
raw body of my request: {"key":"thisbodyrequestisdone"}

So, I would expect to see the var_dump of my body $req parameter sent via client at http://localhost:8000/foo. But it shows the message:

MethodNotAllowedHttpException enter image description here

Probably I'm missing something. Can someone tell me exactly how to make a POST request in the right way within lumen? Thanks in advice.

UPDATE: below some additional screenshot while trying to use the $req->all():

enter image description here

enter image description here

enter image description here

like image 268
UgoL Avatar asked Jun 03 '26 15:06

UgoL


1 Answers

In order for this to work you should type-hint the $req variable, as explained by the docs:

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method. The current request instance will automatically be injected by the service container Source

So your code should be:

$router->post('/foo', function (Request $req) {
    var_dump($req); die();
});

Also make sure you import the Request class with use Illuminate\Http\Request;

like image 64
Sven Hakvoort Avatar answered Jun 05 '26 06:06

Sven Hakvoort