Python send multiple files via post request

Matej

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.

Barmar

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'))
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Send post request with multiple data files and strings in python script

How to send files via post request in python similar to curl? (W3C validator)

POST request via requestJS to send Array of JSON Objects and image files

Send dictionary via http post request from Javascript to python

GoLang send file via POST request

NodeJS: How to Send a file via Request POST

How to send array via postman in POST request

Send button value by post request via jquery

Python not able to send a POST request

How to send multiple request by using post method

Move multiple files uploaded via POST Http request (okhttp 3.3.1) to another directory

how to send media with meta data (caption, description) via POST request to WordPress REST API with python

Flutter how to send multiple files to http post

Python How to send multiple files

multiple asynchronous post request with files in nodejs

How to send raw data in a POST request Python

Python - Unable to send request [error 203, POST]

How to send urlencoded parameters in POST request in python

python can't send post request image

Python - Unable to Send Post Request to login form

Send POST request to mailchimp API with Python

getting error when send a post request in python

Python send a JSON Array via POST

How to send the nested json in POST Request via postman

Send a POST request via Axios to a Firebase Cloud Function

Flask returning 400 error when POST request is send via a XMLHttpRequest

How to send POST request via angular's HttpClient?

Unable to send POST request via HTTP in Angular 4

Send POST Request via AWS Lambda in Node.js