• Plans & Pricing
  • Log in
  • Get started
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Newsletter
  • Professional Email
  • Website Design Services
  • Commerce
  • WordPress Studio
  • Enterprise WordPress 
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • Google Apps
  • Support Center
  • WordPress News
  • Business Name Generator
  • Logo Maker
  • Discover New Posts
  • Popular Tags
  • Blog Search
Get started
  • Sign up
  • Log in
About
  • Plans & Pricing
Products
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Newsletter
  • Professional Email
  • Website Design Services
  • Commerce
  • WordPress Studio
  • Enterprise WordPress  
Features
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • Google Apps
Resources
  • Support Center
  • WordPress News
  • Business Name Generator
  • Logo Maker
  • Discover New Posts
  • Popular Tags
  • Blog Search
Jetpack App
  • Learn more
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
Search
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
Forums / How to upload an image to wordpress.com through REST API in python

How to upload an image to wordpress.com through REST API in python

  • Unknown's avatar
    fadookadoo · Member · Nov 13, 2023 at 12:15 am
    • Copy link Copy link
    • Add topic to favorites Add topic to favorites

    I am trying to upload an image to my wordpress.com site (not WP installed on my own site) using WordPress’ REST API in python. There are a few examples of how to do this on SO when the WordPress site is hosted by the user. But I could not find any example when the site is on wordpress.com. I tried to follow the example of a similar problem here and used the code below. I tried url = 'https://public-api.wordpress.com/wp/v2/sites/' + domain + '/media' as the access URL but got a 400 (malformed) response code.

    I generated wp_token as

    wordpress_credentials = wordpress_user + ':' + wordpress_password
    wordpress_token = base64.b64encode(wordpress_credentials.encode())
    wp_token = wordpress_token.decode('utf-8')

    wordpress_password here is the app password.

    How do I debug why I am getting the 400 response? Appreciate any help.

    import json, os, subprocess
    def wp_upload_image2(domain, wp_token, img_path):
    # Form the url
        url = 'https://public-api.wordpress.com/rest/v1.1/sites/' + domain + '/media/new'
    
    # Extract the filename and the extension from the img_path
       extension = img_path[img_path.rfind('.')+1 : len(img_path)]
       print("img_path: ", img_path, "Filename: ", img_path, "extension: ", extension)
    
    # Now form the header
        headers = {
            'Authorization': 'BEARER ' + wp_token,
        }
    
        try:
            with open(img_path, 'rb') as fp:
                print("Successfully opened file!")
                files = {
                   'media[]': fp,
                }
                rs = requests.post(url, headers=headers, files=files)
                print("Response from WP: ", rs)
                return(rs)
            except IOError as e:
                print(f"Couldn't open file ({e})")
    

    The blog I need help with is: (visible only to logged in users)

  • The topic ‘How to upload an image to wordpress.com through REST API in python’ is closed to new replies.

Tags

  • account
  • design
  • images
  • import
  • Rest API

About this topic

  • In: Support
  • 1 participant
  • 0 replies
  • Last activity 2 years
  • Latest reply from fadookadoo

Couldn't find what you needed?

Contact us

Contact us

Get answers from our AI assistant, with access to 24/7 expert human support on paid plans.

Browse our guides

Browse our guides

Find step-by-step solutions to common questions in our comprehensive guides.

WordPress.com

Products
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Professional Email
  • Website Design Services
  • WordPress Studio
  • Enterprise WordPress
Features
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • Google Apps
Resources
  • WordPress.com Blog
  • Business Name Generator
  • Logo Maker
  • WordPress.com Reader
  • Accessibility
  • Remove Subscriptions
Help
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
  • Developer Resources
Company
  • About
  • Press
  • Terms of Service
  • Privacy Policy
  • Do Not Sell or Share My Personal Information
  • Privacy Notice for California Users
DeutschEspañolFrançaisBahasa IndonesiaItalianoNederlandsPortuguês do BrasilSvenskaTürkçeРусскийالعربيةעִבְרִית日本語한국어简体中文繁體中文English

Mobile Apps

  • Download on the App Store
  • Get it on Google Play

Social Media

  • WordPress.com on Facebook
  • WordPress.com on X (Twitter)
  • WordPress.com on Instagram
  • WordPress.com on YouTube

Automattic

Automattic
Work With Us
    • WordPress.com Forums
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • Manage subscriptions