0

Hello I need to send multiple image files in array via post request in python to my API. Problem is that I always see only one file. Simplified version of code is

import requests

url = 'https://v******p.sk/api/products'

imageFiles = [
    ('images', open('images/1.jpg', 'rb')),
    ('images', open('images/2.jpg', 'rb')),
    ('images', open('images/3.jpg', 'rb'))
]

r = requests.post(url, files=imageFiles)
print(r.text)

API has one images input which accept array of files so I don't want to use something like

imageFiles = [
    ('image1', open('images/1.jpg', 'rb')),
    ('image2', open('images/1.jpg', 'rb')),
    ('image3', open('images/1.jpg', 'rb'))
]

Now I set my API to echo just

print_r($_FILES);

When I try to post files from POSTMAN it works as expected and return something like this:

Array
(
    [images] => Array
        (
            [name] => Array
                (
                    [0] => 1.jpg
                    [1] => 2.jpg
                    [2] => 3.jpg
                )

            [full_path] => Array
                (
                    [0] => 1.jpg
                    [1] => 2.jpg
                    [2] => 3.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /data/4/.tmp/upload/php94o87g
                    [1] => /data/4/.tmp/upload/phpoBeKRB
                    [2] => /data/4/.tmp/upload/phpYCa8dM
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [size] => Array
                (
                    [0] => 112073
                    [1] => 186980
                    [2] => 186980
                )

        )

)

But when I try from python, I get just one file:

Array
(
    [images] => Array
        (
            [name] => 3.jpg
            [full_path] => 3.jpg
            [type] =>
            [tmp_name] => /data/php537KO4
            [error] => 0
            [size] => 154584
        )

)

How to post array of files with same key please? I checked many posts on the web and the first approach should be working, but it's not. Thank you very much in advance.

Matej
  • 3
  • 3
  • I think your code should have worked. See https://stackoverflow.com/questions/18179345/uploading-multiple-files-in-a-single-request-using-python-requests-module – Barmar Apr 27 '23 at 22:02

1 Answers1

0

PHP requires the parameter name to end with [] to create an array of parameter values.

imageFiles = [
    ('images[]', open('images/1.jpg', 'rb')),
    ('images[]', open('images/2.jpg', 'rb')),
    ('images[]', open('images/3.jpg', 'rb'))
]
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for the answer, this would probably work, but API accepts only array of files with name "images". I need to get same output as for example from html input where images is array – Matej Apr 27 '23 at 21:58
  • Hmm. Other answers suggest that your code should have worked. E.g. https://stackoverflow.com/questions/18179345/uploading-multiple-files-in-a-single-request-using-python-requests-module – Barmar Apr 27 '23 at 22:01
  • And this documentation shows similar: https://requests.readthedocs.io/en/latest/user/advanced/#post-multiple-multipart-encoded-files – Barmar Apr 27 '23 at 22:02
  • Ou... great, that's exactly what I needed. Thank you very much. I was trying a lot of combination many hours... THANK YOU – Matej Apr 27 '23 at 22:12