0

I'm trying to send a picture to a server I've made, now the problem is that the following curl code from terminal:

curl -X POST -F 'file=@path' -F 'delete_image=yes' url

where path is the absolute path of the image and url is url of the server

The problem is this code (that should be the exact tranlation in python is not working and it's returning an error 400 "bad request"

files = [
     ('file', open('/Users/viewermac_1/Desktop/Testbed_Web/test.png','rb')),
     ('delete_image', 'yes'),
]

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

Update: Putting delete_image as data works, but I'm not receiving anything back from the server (I'm supposed to receive a link)

MathT
  • 65
  • 7
  • 1
    `('delete_image', 'yes')` isn't a file. Maybe you should pass that part to [**`data`**](http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests). – Peter Wood Jun 25 '17 at 08:35
  • 1
    pass delete_image to data – Stack Jun 25 '17 at 08:38
  • Possible duplicate of [Uploading files using requests and send extra data](https://stackoverflow.com/questions/13015166/uploading-files-using-requests-and-send-extra-data) – Peter Wood Jun 25 '17 at 08:39
  • but the terminal code works even if the delete_image is a -F – MathT Jun 25 '17 at 08:42

1 Answers1

0

Try using this code :

import requests

files = {
    'file': ('path', open('/Users/viewermac_1/Desktop/Testbed_Web/test.png', 'rb')),
    'delete_image': (None, 'yes'),
}

response = requests.post(url, files=files)
Noé
  • 320
  • 2
  • 14