API 3 upload image in python

685 views
Skip to first unread message

Jan Masar

unread,
Sep 20, 2021, 8:51:25 AM9/20/21
to Etsy API
Hello
I'm working with etsy API 3 on my website and I'm stuck on how to upload image to my draft listing in python.

I was tryng all this solutions (try1 to try5):

headers = {
    "Content-Type": "multipart/form-data",
    "x-api-key": settings.ETSY_API_KEY,
    "Authorization": f"Bearer {access_token}"
}

image_file = open('static/images/CACHE/images/product/000063_1-dsc05556/test_file.jpg', 'rb')
read_image_file = image_file.read()
read_image_file_bytes = base64.b64encode(read_image_file)
read_image_file_bytes_str = "".join([format(n, 'b') for n in read_image_file_bytes])

try1 = requests.post(url=url, headers=headers, data={'image': img})
try2 = requests.post(url=url, headers=headers, data={'image': read_image_file})
try3 = requests.post(url=url, headers=headers, data={'image': read_image_file_bytes})
try4 = requests.post(url=url, headers=headers, data={'image': read_image_file_bytes_str})

# trying to set existing image from my other listing
try_5 = requests.post(url=url, headers=headers, data={'listing_image_id': 2853097528})

but I always get the same response:
{'error': 'Either a valid image file or a listing_image_id must be provided.'}

Could you please let me know what I'm doing wrong?

Thank you
Jan

kipruto

unread,
Sep 24, 2021, 8:36:14 AM9/24/21
to Etsy API
This is a common problem. I'm also getting the same error using node js. Perhaps the team should really look into it and get back.

kipruto

unread,
Sep 24, 2021, 8:36:14 AM9/24/21
to Etsy API
Hello,
Did you manage to solve this? I'm getting a similar error using node Js?
Here is what I've tried:

```
        const img = fs.readFileSync('./test.png');
        var urlencoded = new URLSearchParams();
        urlencoded.append("image", img)

        const requestOptions = {
            method: "POST",
            headers: {
                Authorization: `Bearer ${token}`,
                "x-api-key": clientID,
                "Content-Type": "multipart/form-data"
            },
            body: urlencoded,
        }
        const res = await fetch(url, requestOptions);
        const imageData = await res.json();


```
On Monday, September 20, 2021 at 12:51:25 PM UTC johny...@hotmail.com wrote:

Jan Masar

unread,
Sep 30, 2021, 2:56:08 PM9/30/21
to Etsy API
Hi,
No, I have not find PYTHON solution for my problem yet. When I'll find out I'll post it.

Julio Camejo Corona

unread,
Oct 27, 2021, 12:59:32 PM10/27/21
to Etsy API
Hello,
Any solution, please?

Jan Masar

unread,
Nov 1, 2021, 9:04:54 AM11/1/21
to Etsy API
I have no solution yet. Right now I'm working on something else. If you find the solution feel free to share. Thank you ;) 

Julio Camejo Corona

unread,
Nov 18, 2021, 10:05:46 AM11/18/21
to Etsy API
Hello, I found the solution. The main key to the problem is sending "Content-Type" and that it's causing the error. My final code is:
files = {
"image": open(file_path, 'rb')
}
headers = {
"Authorization": "Bearer " + user_token,
"x-api-key": X_API_KEY
Reply all
Reply to author
Forward
0 new messages