psf / psf/requests

Multipart post of image does not get the same result in requests as in Insomnia/Postman

Open
#5,202 4 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.