1

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());
    }
}
Martijn
  • 73
  • 1
  • 6

3 Answers3

1

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
)
runz0rd
  • 126
  • 5
  • Well that is the weird thing: when I set the Content-Type to application/json the response is empty. When I set it to text/plain, though, and output `$request->json()`, the correct data is outputted. Could it have something to do with the dingo API package? – Martijn May 24 '16 at 07:40
  • Ok we figured out dingo was not the problem; when turned off the request object is still initiated twice – Martijn May 24 '16 at 08:30
0

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);
naschans
  • 28
  • 1
  • 6
  • That did the trick! Not sure why I had `Illuminate\Http\Request::capture();` in the first place, but anyway – Martijn May 24 '16 at 19:48
0

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

application/x-www-form-urlencoded