8
votes

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"

5
No error, only a response of "NULL" Cant use getRealPath() etc since nothing gets uploadeduser2722667
You are not returning anything. You're just var_dump'ing data. Try returning json instead.dan-klasson
Can you access that controller from a browser? Maybe it's throwing an exception and you've got debug turned off.dan-klasson
please upgrade the postman if you do not have the latest versionKalhan.Toress
I've tried as json, same returns NULL. And I got the latest postman, the packaged app aswell. Can you post files with the code above?user2722667

5 Answers

40
votes

If you want to test file uploads with Postman and Laravel, simply remove the Content-Type header setting in Postman.

5
votes

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.

1
votes

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

0
votes

Possible Answer Could not submit form-data through postman put request

Headers: Content-Type application/x-www-form-urlencoded

return $request->file('avatar');

image example

enter image description here

0
votes

I've just delete Headers fields and worked to me