Multipart post of image does not get the same result in requests as in Insomnia/Postman
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 54.3k
- Forks
- 10.4k
- Avg merge
- 16h 43m
- Merged PRs (30d)
- 3
Description
I am trying to upload a .jpg file to a server using HTTP POST with the requests library in Python 3.7.
Target URL has some PHP code that handles the upload, taking 'fileToUpload' as the multipart variable.
I tried putting the request into a with-statement for opening the file, changing the data=files to files=files (as recommended by some example code), or setting the headers to multipart/form-data specifically.
import requests
url = 'http://someurl.com/upload/dir/post.php'
files = {'fileToUpload' : open('image.jpg', 'rb')}
r = requests.post(url, data=files)
If I run the script I raise every single error message in the post.php file, while if I take the thing to Insomnia or Postman the upload works just fine, so the server-side seems to be working.
After all it seems that the problem is with opening the file, but I tried to stick to the documentation and still can't figure out what is wrong here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the documented multipart example with the shown requests.post call, especially the use of the files argument and the file field name. Reproduce the upload against the reported PHP endpoint or an equivalent test server, and consider the issue resolved only when the request produces the same upload result as Insomnia or Postman.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100