laravellumendingo-api

Getting PUT data in Lumen


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());
    }
}

Solution

  • 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);