user2722667
user2722667

Reputation: 8661

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"

Upvotes: 9

Views: 29693

Answers (5)

Martinho Mussamba
Martinho Mussamba

Reputation: 467

I've just delete Headers fields and worked to me

Upvotes: 0

ztvmark
ztvmark

Reputation: 1448

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

Upvotes: 0

bruno
bruno

Reputation: 148

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.

Upvotes: 6

Ashish Kumar
Ashish Kumar

Reputation: 76

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

Upvotes: 1

MrSnoozles
MrSnoozles

Reputation: 920

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

Upvotes: 43

Related Questions