dd($request->all()); gives back empty array
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);
}
}
Related videos on Youtube
chloealee
Updated on July 25, 2022Comments
-
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 almost 9 yearsShow your use statement please, code looks fine so maybe you imported the wrong request class?
-
chloealee almost 9 yearshere's my use statement:
use Northstar\Services\AWS; use Northstar\Models\User; use Illuminate\Http\Request; use Validator; use Input; use Response;
-
Borjante almost 9 yearsIt's ok, and surely you are sending some kind of data throught postman?
-
chloealee almost 9 yearsyes! 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 almost 9 yearsThanks 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 about 8 yearscould some one illustrate how to do this ?
-
mercury about 8 yearsNon-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context
-
mercury about 8 yearsRequest {#41 ▼ #json: null
-
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 over 6 yearsThis did not work for me though it seems to be the method that works for almost everyone
-
ssfinney almost 6 years$request->all() is a perfectly fine way to reference the request's attributes.
-
Dave Howson over 3 yearsThis 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.