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
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():



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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With