Jan Mark
Jan Mark

Reputation: 167

how to create an image _FILES array using python to upload image

I'm creating a script to upload listing image to etsy using python.

However, when I executed the could it return a message "The image array metadata doesn't look like a _FILES array"

BTW, I'm using a library https://github.com/mcfunley/etsy-python

I tried this code:

product_image = open(filename)

result = etsy.uploadListingImage(listing_id=listing_id, image=product_image)

But it would return an error message "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"

I tried another:

product_image = {filename, open(filename, 'rb'), 'image/jpeg'}

result = etsy.uploadListingImage(listing_id=listing_id, image=product_image)

But it would return an error message "The image array metadata doesn't look like a _FILES array"

There is a sample code for php (https://www.etsy.com/developers/documentation/getting_started/images#section_uploading_images) but I had difficulty conforming it to the python library that I use.

$source_file = dirname(realpath(__FILE__)) ."/$filename";

$url = "https://openapi.etsy.com/v2/listings/".$listing_id."/images";
$params = array('@image' => '@'.$source_file.';type='.$mimetype);

$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST);

$json = $oauth->getLastResponse();

So I think the main issue is the variable type or structure of the image param of the uploadListingImage.

Upvotes: 0

Views: 352

Answers (1)

robertop23
robertop23

Reputation: 34

Something like this should work:

from requests_oauthlib import OAuth1Session

etsy = OAuth1Session(
    api_key,
    shared_secret,
    oauth_token, 
    oauth_token_secret
)

url = f'https://openapi.etsy.com/v2/listings/{listing_id}/images'
files = { 'image': (open(image_path, 'rb') }

response = etsy.post(url, files = files)

Upvotes: 1

Related Questions