Post a file

import requests

    #Send POST
    cloud_url = 'https://some_domain.com/some_file'
    files = {'file': open('my_image.jpg', 'rb')}
    response = requests.post(cloud_url, files=files)
    if response.status_code == 200:
        print("Success: \n" + response.text)
    else:
        print("Error: " + response.status_code)

Post a file from binary data

I.e, POST without needing to save your data to a file first

import requests
import io

    image1_byte_array = io.BytesIO()
    image1.save(image1_byte_array, format='JPEG')
    image1_byte_array = image1_byte_array.getvalue()

    #Send POST
    cloud_url = 'https://some_domain.com/some_file'
    files = {'file': open('my_image.jpg', 'rb')}

        #files = {'file': image_data}                           #You can use this to send just the binary data as the file content
        files = {'file': ('1.jpg', image_data, 'image/jpeg')}   #You can use this to also pass a file name and type if the server wants to see these to handle the file correctly
        response = requests.post(cloud_server_url, files=files)

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *