• 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 / Uploading images via API

Uploading images via API

  • Unknown's avatar
    earnedmediavideo · Member · Dec 17, 2024 at 7:36 pm
    • Copy link Copy link
    • Add topic to favorites Add topic to favorites

    Hi there,

    I’m attempting to build a site that takes semi-automated content via API.

    I have had no difficulty stringing together code to make text posts work, but I cannot find a way to upload images. Following multiple guides, I assembled a python function that send the image via the “media” endpoint.

    When I do, I get the following error: {“code”:”rest_upload_sideload_error”,”message”:”Sorry, you are not allowed to upload this file type.”,”data”:{“status”:500}}

    This is what the function looks like at present:

    def upload_image_to_wordpress(file_object, file_name):
    Parameters:
        file_object (BytesIO): The image file as a BytesIO object.
        file_name (str): The name of the image file (e.g., 'plot.png').
    
    Returns:
        dict: JSON response from the WordPress API.
    """
    # Define the API endpoint for media uploads
    media_endpoint = f"https://public-api.wordpress.com/wp/v2/sites/{os.environ['WP_SITE_URL']}/media"
    
    headers = handle_oauth()
    
    headers['Content-type'] = 'image/jpeg' 
    headers['Content-disposition'] = 'attachment; filename=%s'% file_name
    
    # Make the POST request to upload the image
    response = requests.post(media_endpoint, headers=headers, data=file_object)
    
    # Check for success
    if response.status_code == 201:  # HTTP 201 Created
        print("Image uploaded successfully!")
        return response.json()
    else:
        print(f"Failed to upload image: {response.status_code}")
        print(response.text)
        return None


    I have investigated the site backend, the settings for the app, and exhaustively queried support forums and ChatGPT. I cannot find an answer.

    How do I get past this?

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

  • The topic ‘Uploading images via API’ is closed to new replies.

Tags

  • account
  • design
  • images

About this topic

  • In: Support
  • 1 participant
  • 0 replies
  • Last activity 1 year
  • Latest reply from earnedmediavideo

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