dd($request->all()); gives back empty array

40,720

Solution 1

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!

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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);
    }
}
Share:
40,720

Related videos on Youtube

chloealee
Author by

chloealee

Updated on July 25, 2022

Comments

  • chloealee
    chloealee almost 2 years

    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 northstar.com/users/{id}/avatar
     */
      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);
      }
    }
    
    • Borjante
      Borjante almost 9 years
      Show your use statement please, code looks fine so maybe you imported the wrong request class?
    • chloealee
      chloealee almost 9 years
      here's my use statement:use Northstar\Services\AWS; use Northstar\Models\User; use Illuminate\Http\Request; use Validator; use Input; use Response;
    • Borjante
      Borjante almost 9 years
      It's ok, and surely you are sending some kind of data throught postman?
    • chloealee
      chloealee almost 9 years
      yes! sending both a file (jpg image) and text data but nothing is showing up in the array dd($request->all()); sends. If I comment that out, this error is thrown, even though there is a photo in the photo field: {"error":{"photo":["The photo field is required."]}}
  • chloealee
    chloealee almost 9 years
    Thanks all! Unfortunately, still an empty array/throwing an error. I'm thinking now that the request isn't coming through but since I am entering data in Postman and using Laravel's Request, shouldn't it be gathering the data?
  • mercury
    mercury about 8 years
    could some one illustrate how to do this ?
  • mercury
    mercury about 8 years
    Non-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context
  • mercury
    mercury about 8 years
    Request {#41 ▼ #json: null
  • chloealee
    chloealee about 8 years
    @HosMercury if you open Postman and under the URL you should see four tabs - Authorization, Headers, Body, Pre-request script, and Tests. Click on the Headers tab and you should be able to type in the above! "Accept" goes in the left hand column and in the right column on the same row, you'd write "application/json" and same for "Content-Type" and again, "application/json" under on the next row. Hope this helps!
  • Pila
    Pila over 6 years
    This did not work for me though it seems to be the method that works for almost everyone
  • ssfinney
    ssfinney almost 6 years
    $request->all() is a perfectly fine way to reference the request's attributes.
  • Dave Howson
    Dave Howson over 3 years
    This is an important point because Postman does not explicitly let you know that the JSON is malformed. Instead, it draws a curly underline under the malformed component which could be easily missed.