How upload img by ETSY API in listing

492 Views Asked by At

I try upload img on Etsy listing, all time this error:

Either a valid image file or a listing_image_id must be provided.

Here my Python code:

payload = {'image': open("103151318_"+str(i)+".jpg", 'rb'), 'name': "image.jpg"}
headers = {
    'Content-Type': 'multipart/form-data',
    "x-api-key": self.keystring,
    "Authorization": "Bearer " + self.token,
    'Content-Disposition': 'form-data; name="image"; filename="image.jpg"'
}

data = {"rank": rank}

response = requests.post(uri, headers=headers, files=payload, params=data)

I tried and changed code from PHP to Python from Etsy instruction (from here)

This code:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://openapi.etsy.com/v3/application/shops/xxxxxxxx/listings/yyyyyyyy/images',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('image'=> new CURLFILE('/path/to/my/image.jpg')),
  CURLOPT_HTTPHEADER => array(
    'Content-Type: multipart/form-data',
    'x-api-key: 112233445566778899',
    'Authorization: Bearer abcd1234efgh5678ijkl90mnopqrst'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
1

There are 1 best solutions below

0
Noah Overcash On

It's undocumented, but you'll need to supply a post field name with the filename. It's missing from the official docs, but you can see it mentioned in this Google Group thread and this GitHub discussion thread.