1

I need to upload a local picture to an URL. My curl request looks like : "curl -T 'my_picture' 'the_url' ".

My purpose is to do it in Python, I saw, that I have to use : requests.put(). Everything I did before works nice, but this function give me a lot of trouble.

Solution is probably really easy, but I'm lost.

Thanks for any help.

def apithree(urlupload):
    url = urlupload
    picture = Image.open("test.png")
    headers = {"content-type": "multipart/form-data"}

    response = requests.put(url, headers=headers, data=picture)
    response = response.json()
    print(response)

Other Code I tried

def apithree(urlupload):
    url = urlupload
    files = {'image': open('test.png', 'rb')}

    response = requests.post(url, data=files)
    response = response.json()
    print(response)

If the command works,the output should be empty, but I always have error messages.

I can add error messages if necessary.

Thanks for any help.

Vindyd
  • 11
  • 1

1 Answers1

0

If server accepts multipart/form-data images, you probably need files= parameter for your request:

import requests

files = {'file_name': open(r'C:\data\...\image.png', 'rb')}
r = requests.post(YOUR_URL, files=files)  # or requests.put(...)
print(r.status_code, r.json())
Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
  • Thanks for u answer. I tried this one, but I have this error message : requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection must have been closed by the remote host', None, 10054, None)) – Vindyd Jul 04 '19 at 14:19
  • Are URLs in `curl` and `requests` the same? Are they using https? – Ivan Vinogradov Jul 04 '19 at 14:23
  • relevant question: https://stackoverflow.com/questions/8814802/python-errno-10054-an-existing-connection-was-forcibly-closed-by-the-remote-h – Ivan Vinogradov Jul 04 '19 at 14:27
  • Just try to run this code again. If it still gives you 10054 error, check if you can make any other requests to this URL (GET with `requests` and `curl` for example). – Ivan Vinogradov Jul 04 '19 at 14:38
  • Yes, that's the same URL, I print my url and I tried myself with Curl and it works. For the 2nd point, I also add "time.sleep(2)" in each part, to generate less requests simultaneous, but still this error. I will try to do my request on curl in another format hoping it works. If I found something, I will post it. – Vindyd Jul 04 '19 at 14:47
  • Ok, I find this is working : url = urlupload files = {'file': ('test.png', open('test.png', 'rb'))} requests.put(url, files=files) / But, the picture is impossible to see on the API, dont understand why, but this time, I can send my picture :) Thanks for your time and your help ! :) – Vindyd Jul 04 '19 at 15:21
  • well, that's exactly what i wrote in my answer) – Ivan Vinogradov Jul 04 '19 at 15:24
  • Yes, I just modify the .post to a .put. But this is your answer. Now, I'm trying to add some informations on the .put{} to see my picture. – Vindyd Jul 04 '19 at 15:31