Send file with postman to Laravel API

25,343

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
25,343
user2722667
Author by

user2722667

Updated on March 23, 2021

Comments

  • user2722667
    user2722667 about 3 years

    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"