'Python Requests: POST form-data with empty file field
Python 3.x, Requests 2.18.4
Trying to reproduce the following multi-part form-data request in python's requests library
Just need to post the URL, but the other field is required else the sedrver responds with an error.
Doing the following doesn't work
requests.post(target, files={'img[]': ('', '', 'application/octet-stream'),'url': (None, url)})
Requests doesn't create a request.body when I execute the above. Can't find a way to post the form-data with both fields, without adding a file.
Is there any way to do this?
Solution 1:[1]
In case anyone is still looking for the answer to this. the easiest way is to just pass an empty dictionary to the files parameter:
requests.post(url,..., files={})
there is also another workaround found here (https://github.com/psf/requests/issues/1081) where you can generate an object of this class and pass it as the content for files:
class ForceMultipartDict(dict):
def __bool__(self):
return True
FORCE_MULTIPART = ForceMultipartDict() # An empty dict that boolean-evaluates as `True`.
client.post(url, data={"some": "data"}, files=FORCE_MULTIPART)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Agustin Carpaneto |

