1
votes

I'm struggling to create working PUT routes in my Lumen API. My requests reach the correct route, but I'm unable to access any values through $request->all().

I've figured out that PHP needs to read the php://input stream to get the PUT body. This is done in the getContent() function of Symfony\Component\HttpFoundation\Request. However, this function is called multiple times and since the input buffer is emptied when read, the data is not present when I need it.

I also found out that when I set my Content-Type header to text/plain I can successfully print $request->json(), but when I set it to application/json the object is empty.

Any ideas?

PS: I'm aware that for HTML requests you should add the _method parameter, but since I'm not doing my requests through HTML the parameter shouldn't be needed.

Edit:

My route:

$app->put('settings', 'SettingController@update');

My controller:

class SettingController extends Controller
{
    public function update(Request $request)
    {
        print_r($request->all());
    }
}
3
Could you show your code? - user3520734
Which part? In my routes.php: $api->put('settings', 'SettingController@update');. In the update method I simply do print_r($request->all()), which is empty. I'm using the dingo package by the way. - Martijn
Show your route and controller if you're using one. - user3520734
I edited my original post - Martijn

3 Answers

1
votes

The Content-Type of your request needs to be set accordingly. In my case i was sending json data, so ive set it to application/json.

So when i sent this in my request body:

{"asd": "asdf"}

I got this response with the code from your controller:

Array
(
    [asd] => asdf
)
0
votes

I've had this exact same problem with Lumen. In public/index.php I had to replace:

Illuminate\Http\Request::capture();

$app->run($request)

with this:

$app->run();

Note: If you want to use the Request object after $app->run() (for example to do some logging or benchmarking) you should use $app->request.

logging_function($app->request);
0
votes

You need to have the correct content type in your header. try

application/x-www-form-urlencoded