Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file with postman to Laravel API

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"

like image 559
user2722667 Avatar asked Jan 22 '15 16:01

user2722667


3 Answers

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

like image 122
MrSnoozles Avatar answered Nov 16 '22 07:11

MrSnoozles


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.

like image 6
bruno Avatar answered Nov 16 '22 05:11

bruno


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

like image 1
Ashish Kumar Avatar answered Nov 16 '22 05:11

Ashish Kumar