• Resolved antox2010

    (@antox2010)


    This is my python function to save images and upload them on my website

    def uploadImage(image_url, sku):
    
        url='https://website.com/wp-json/wp/v2/media'
    
        user_agent = UserAgent().random
    
        headers = {"User-Agent": user_agent}
    
        #Image download
    
        img_data = requests.get(image_url, headers=headers).content
    
        with open(f'{sku}.jpg', 'wb') as handler:
    
            handler.write(img_data)
    
        media = {'file': open(f'{sku}.jpg',"rb")}
    
        user = "user"
    
        password = "XXXX XXXX XXXX XXXX XXXX XXXX"
    
        credentials = user + ':' + password
    
        token = base64.b64encode(credentials.encode())
    
        res = requests.post(url= url,
    
                            headers={ 'Authorization': 'Basic ' + token.decode('utf-8')},
    
                            files= media,
    
                           )
    
        print(res)

    For the auth I use Application Passwords plugin

    res?is 200 and if I print?res.json()?it returns a list of the images I have on my website, but the image is not uploaded, there is no error in the response or the image filename.

    Tried with different users and also disabling all plugins, but nothing changed. Tried also with auth with username and password and had the same problem

    Current version: 6.1.1

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not that good with Python, but it looks like you’re missing some crucial header information. An example that supposedly works from StackOverflow:

    def restImgUL(imgPath):
        url='https://xxxxxxxxxxxx.com/wp-json/wp/v2/media'
        data = open(imgPath, 'rb').read()
        fileName = os.path.basename(imgPath)
        res = requests.post(url='https://xxxxxxxxxxxxx.com/wp-json/wp/v2/media',
                            data=data,
                            headers={ 'Content-Type': 'image/jpg','Content-Disposition' : 'attachment; filename=%s'% fileName},
                            auth=('authname', 'authpass'))
        # pp = pprint.PrettyPrinter(indent=4) ## print it pretty. 
        # pp.pprint(res.json()) #this is nice when you need it
        newDict=res.json()
        newID= newDict.get('id')
        link = newDict.get('guid').get("rendered")
        print newID, link
        return (newID, link)

    I hope this helps some.

    Thread Starter antox2010

    (@antox2010)

    I already tried it before asking here and got the same result. So 200 but the image is not uploaded

    Moderator bcworkz

    (@bcworkz)

    For the auth I use Application Passwords plugin

    The plugin is not necessary with v6.1.1, functionality is now built into core WP. I wonder if the two together is causing some sort of validation conflict. Deactivate the plugin and create a new password in a user profile that has adequate upload capability. If the current password from the plugin should appear in the profile, I recommend removing it and using the new one. The current one may still be valid without the plugin, but why chance it? IMO better to stay fully within the core password scheme.

    There are a number of cURL examples that do work. You could use one of them to upload, with the goal being to examine what sort of request cURL is sending, in particular which headers. Configure your Python to send exactly the same sort of request.

    Thread Starter antox2010

    (@antox2010)

    I found out that I get 200 and the list of images because when I POST wp-json/wp/v2/media I get a redirect 301. Still don’t know how to solve this

    Thread Starter antox2010

    (@antox2010)

    Found the problem… there was a redirect from mywbsite.com to https://www.mywebsite.com.

    Using www works

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Can’t upload image on wordpress with python api’ is closed to new replies.