When I try to send a file with postman to my Laravel api the result always returns "NULL"
my controller look like this:
public function store()
{
$file = Input::file('image');
var_dump($file);
}
And my route file:
Route::post('test', 'TestController@store');
But when I try to send a image in postman I get the result "NULL"
My postman config is:
http://app.dev/test (POST)
Content-Type - multipart/form-data
form-data
image - test.jpg
Am I missing anything?
I've checked php.ini so that I have enough space for uploads
My problem is exactly like the one in this post: Correct Postman Settings when testing file uploading in Laravel 4?
But his solution didnt work. I have already checked my php.ini
The code bellow works if I use a resource controller and enter the url form a browser:
public function store()
{
$input = Input::all();
$path = public_path() .'/images/123/';
$inpusqt = Input::file('images');
$i = 1;
File::exists($path) or File::makeDirectory($path);
foreach($inpusqt as $file) {
$filExt = $file->getClientOriginalExtension();
$ext = '.' . $filExt;
$lol = Image::make($file->getRealPath());
$lol->heighten(258)->save($path . $i . $ext);
$i++; //increment value to give each image a uniqe name
}
}
But if I modify my route to eg post::(controller@store) and send the images with postman it get an error saying that "Undefined index: images"
If you want to test file uploads with Postman and Laravel, simply remove the Content-Type
header setting in Postman.
If you send request to upload a file form Postman you should include in the postman also a header Content-Type: multipart/form-data and select form-data, there the field should be file and not text . Also be careful only with POST you can make file upload with PUT and PATCH it doesn't function.
When choosing "form-data" in PostMan, it automatically sets a header "Content-Type: application/x-www-form-urlencoded".
I just removed the header, and everything worked :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With