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

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!

3

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"
}
1

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).

2

try use this

dd($request->all());
1

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:

1
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest