Dd($Request->All()); Gives Back Empty Array
I Am Trying to Upload a Photo from My Laravel 5 App to Be Stored in Aws. I Am Using the Postman Rest Client to Test. When I Upload a Photo, the Request Returns...
I am trying to upload a photo from my Laravel 5 app to be stored in AWS. I am using the Postman REST client to test. When I upload a photo, the request returns an empty array. Does anyone know why this might be? Here's the code for my Avatar Controller:
class AvatarController extends Controller
{
public function __construct(AWS $aws)
{
$this->aws = $aws;
}
/**
* Store a new avatar for a user.
* POST
*/
public function store(User $user, Request $request)
{
dd($request->all());
// dd($request->file('photo'));
$file = $request->file('photo');
// $file = Request::file('photo');
// $file = Input::file('photo');
$v = Validator::make(
$request->all(),
['photo' => 'required|image|mimes:jpeg,jpg|max:8000']
);
if($v->fails())
return Response::json(['error' => $v->errors()]);
$filename = $this->aws->storeImage('avatars', $file);
// Save filename to User model
$user->avatar = $filename;
$user->save();
// Respond to user with success
return response()->json('Photo uploaded!', 200);
}
}
Found the answer - looks like there was an issue with my headers in Postman. I had both Accept application/json and Content-Type application/json. Once I removed Content-Type, all is fixed. Thanks!
Bit late to the party, but might be usefull for others:
My problem was that the Content-Type header value was application/json while the actual payload was form data. Changing the header to application/x-www-form-urlencoded fixed the issue.
Just want to add where i made a mistake. When sending POST request with Postman, you also have to make sure the json is correct.
// invalid json (notice the ending comma)
{
"akey":"aval",
"nkey": "bval",
}
//valid json (no ending comma)
{
"akey":"aval",
"nkey": "bval"
}
The same issue on postman, while using PUT/PATCH method directly. One possible solution is to send your PUT/PATCH request as a POST, but include proper _method in the request body along with other parameters.
_method: PUT
Hope this helps
You might want to test that the JSON is valid
$data = $request->all();
if(empty($data)) {
$data = json_decode($request->getContent());
$data = json_decode($data);
if(is_null($data)) {
return response()->json("Not valid json", 400);
}
}
Try dd($request), dd($_REQUEST), dd($request->files).
try use this
dd($request->all());
Although it wasn't the case in OP's question, this problem can also occur if you extend Laravel's Request class with your own.
public function action(\App\Http\Request $request) {
dd($request->input()); // This might be empty
}
In that case you can solve it by adding a line like this in your AppServiceProvider:
$this->app->alias('request', \App\Http\Request::class);
simply change in your Rout file
Route::get('/users', 'AvatarController@store');
to
Route::POST('/users', 'AvatarController@store');
Should be using:
Request::all();
As per documents: