Unable to upload image "error: upload failed (422)" (Python)

Hello,

I’m trying to upload an image to my website, but that does not work. I got an error 422.

Here is my code :

def imageUpload(image_name):
    print("IMAGE UPLOAD")
    token = ghostToken.generate_ghost_token()
    headers = {
               'Content-Type': 'application/json',
               'Authorization': 'Ghost {}'.format(token),
               "Content-Type" : 'multipart/form-data;'
    }
    
    url = config.ghost_admin_url + 'images/upload/' # https://xxxxxx/ghost/api/admin/images/upload/
    files = {"file": (image_name, open(image_name, 'rb'), 'image/png')}
    params = {'purpose': 'image', 'ref': image_name}   # 'image', 'profile_image', 'icon'

    result = requests.post(url, files=files, params=params, headers=headers)

    if result.ok: 
        result = 'success: '+json.loads(result.text)['images'][0]['url']
    else: 
        print(result)
        print(result.text)
        result = 'error: upload failed (' + str(result.status_code) + ')'

    return result

And the output :
IMAGE UPLOAD
<Response [422]>
{“errors”:[{“message”:“Please select an image.”,“context”:null,“type”:“ValidationError”,“details”:null,“property”:null,“help”:null,“code”:null,“id”:“167546b0-1104-11ee-8603-512072010742”,“ghostErrorCode”:null}]}

Thank you in advance.

Have you figured it out? I’m facing the same problem:

Take a look at this, @42piratas ?

1 Like

For future reference, I just had to remove 'Content-Type': 'multipart/form-data;', from my header.

Thank you @Cathy_Sarisky :pray:t4:

1 Like