Skip to content

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

@ClerkCunt

Description

@ClerkCunt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions