Framework: Input::get() not reading JSON

Created on 6 Aug 2014  路  10Comments  路  Source: laravel/framework

If I look at Illuminate\Http\Request, it doesn't appear to override get() from it's superclass.

As a result, when I do get() when the request content type is JSON, I end up not getting any data.

What I'm expecting is all the JSON data to be rolled in with all the other variables in Request.

All 10 comments

Shouldn't you be using Request::json() to get the JSON payload https://github.com/laravel/framework/blob/4.2/src/Illuminate/Http/Request.php#L468

You're missing the point.

Request::get() claims to abstract all submitted data. But in cases where the data is submitted via JSON, it's failing to include the information.

On endpoints where I want to be content-type agnostic, I'm forced to write a detection and extra code to do what Request::get claims to offer.

@atrauzzi I think you can actually use Request::input('key') to get the data regardless of type. I can't find anything about it in the docs, but looking at the code and at http://laravel.com/api/source-class-Illuminate.Http.Request.html#176-188 it seems that way.

get actually falls back to the Symphony Request class, so maybe there's a reason it wasn't overridden, but I can't think of any. But input() has some niceties from what I can see like like allowing you to get files, and handling whether or not that data was JSON properly.

My original issue states that the Symfony method isn't overridden, so I already knew that.

What I'm looking for is a clearer explanation as to why it's not and - provided you are correct - that input is the correct substitute.

@taylorotwell ?

Input::get() is actually an alias to Request::input(), not Request::get().

Confusing! :)

Ummm still an issue I feel with Laravel 5 - cannot pass json to controller. Only URL parameters.

@ezos86

  • set http request header Content-Type: application/json
  • make sure trim all spaces and tabs in json data. use
{"name":"Micheal","age":26}

instead of

{
  "name":"Micheal",
  "age":26
  }
  • dump Input::all()

if you send json like this: with Content-type application/json ,
{"user":{"name":"abc","code":134}}

in laravel controller:
$data = $request->json()->all(); //read json in request
return response()->json($data); //send json respond

I know it's an old post but just providing a little more info if someone comes across it.
Passing JSON works as expected with

Input::all();
$request->all();
// and 
$request->json()->all();

From what I'm seeing, the request 'get' method DOES give you access to all data, however, it gives this access by key. The key is the one required parameter. So, in the above example,

$request->get('user');

To access the entire request, you need the 'all' method.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YannPl picture YannPl  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

digirew picture digirew  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments