Title: Can&#8217;t upload image on wordpress with python api
Last modified: December 17, 2022

---

# Can’t upload image on wordpress with python api

 *  Resolved [antox2010](https://wordpress.org/support/users/antox2010/)
 * (@antox2010)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/)
 * This is my python function to save images and upload them on my website
 *     ```wp-block-code
       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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16299369)
 * 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](https://stackoverflow.com/questions/43915184/how-to-upload-images-using-wordpress-rest-api-in-python):
 *     ```wp-block-code
       def restImgUL(imgPath):
           url='http://xxxxxxxxxxxx.com/wp-json/wp/v2/media'
           data = open(imgPath, 'rb').read()
           fileName = os.path.basename(imgPath)
           res = requests.post(url='http://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](https://wordpress.org/support/users/antox2010/)
 * (@antox2010)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16300009)
 * I already tried it before asking here and got the same result. So 200 but the
   image is not uploaded
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16300556)
 * > 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](https://wordpress.org/support/users/antox2010/)
 * (@antox2010)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16304014)
 * 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](https://wordpress.org/support/users/antox2010/)
 * (@antox2010)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16304050)
 * Found the problem… there was a redirect from mywbsite.com to [http://www.mywebsite.com](http://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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [antox2010](https://wordpress.org/support/users/antox2010/)
 * Last activity: [3 years, 5 months ago](https://wordpress.org/support/topic/cant-upload-image-on-wordpress-with-python-api/#post-16304050)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
