Home:ALL Converter>Python requests multiple form files post

Python requests multiple form files post

Ask Time:2021-05-20T01:44:24         Author:Rodrigo

Json Formatter

I have the following cURL request and I want to mimic the form files into requests API

curl --request POST \
    --url http://localhost:3000/convert/html \
    --header 'Content-Type: multipart/form-data' \
    --form [email protected] \
    --form [email protected] \
    --form [email protected] \
    -o result.pdf

My current code is:

files = {
  "files": [
    ("header.html", io.BytesIO(header)),
    ("index.html", io.BytesIO(html)),
    ("footer.html", io.BytesIO(footer)),
  ]
}

response = requests.post(url, files=files)

How can I pass multiple files into the form using requests?

  File "main.py", line 39, in main
    response = requests.post(url, files=files)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/sessions.py", line 590, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/sessions.py", line 528, in request
    prep = self.prepare_request(req)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/sessions.py", line 456, in prepare_request
    p.prepare(
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/models.py", line 319, in prepare
    self.prepare_body(data, files, json)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/models.py", line 507, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/requests/models.py", line 166, in _encode_files
    rf.make_multipart(content_type=ft)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/urllib3/fields.py", line 268, in make_multipart
    self._render_parts(
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/urllib3/fields.py", line 226, in _render_parts
    parts.append(self._render_part(name, value))
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/urllib3/fields.py", line 206, in _render_part
    return self.header_formatter(name, value)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/urllib3/fields.py", line 117, in format_header_param_html5
    value = _replace_multiple(value, _HTML5_REPLACEMENTS)
  File "/home/ubuntu/workspace/report/venv/lib/python3.8/site-packages/urllib3/fields.py", line 90, in _replace_multiple
    result = pattern.sub(replacer, value)
TypeError: expected string or bytes-like object

Author:Rodrigo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67608319/python-requests-multiple-form-files-post
simpleApp :

one of these options will work:\nsend as a dictionary:\nfiles_as_dict={'file1':("file1",open('out.png', 'rb'),'image/png'),\n 'file2':("file2",open('out.png', 'rb'),'image/png')\n }\n\n2nd send as list:\nfiles_as_list = [('file', open('out.png', 'rb')), ('file', open('out.png', 'rb'))]\n\nso post request would be\nresponse = requests.request("POST", url, headers=headers, files=files_as_dict)\n#or\n#response = requests.request("POST", url, headers=headers, files=files_as_list)\n",
2021-05-19T18:09:43
yy