Skip to content

Conversation

@Ref34t
Copy link

@Ref34t Ref34t commented Aug 10, 2025

The wp_upload_media tool was sending invalid JSON to WordPress when both file data and metadata (title, alt_text, caption, description) were provided. The issue was caused by the preCallback setting a raw binary body while the REST handler tried to add metadata parameters separately, creating malformed requests.

This fix restructures the wp_upload_media_pre_callback to:

  • Create proper multipart/form-data format that includes both file and metadata
  • Use correct Content-Type header with boundary
  • Clear the args array since everything is now in the multipart body
  • Prevent parameter conflicts that caused JSON validation errors

Changes:

  • Modified wp_upload_media_pre_callback() to generate RFC-compliant multipart bodies
  • Added comprehensive tests for multipart format validation
  • Added test for complete metadata handling (title, caption, description, alt_text)
  • Ensured backward compatibility with existing functionality

Resolves #89

The wp_upload_media tool was sending invalid JSON to WordPress when both file data and metadata (title, alt_text, caption, description) were provided. The issue was caused by the preCallback setting a raw binary body while the REST handler tried to add metadata parameters separately, creating malformed requests.

This fix restructures the wp_upload_media_pre_callback to:
- Create proper multipart/form-data format that includes both file and metadata
- Use correct Content-Type header with boundary
- Clear the args array since everything is now in the multipart body
- Prevent parameter conflicts that caused JSON validation errors

Changes:
- Modified wp_upload_media_pre_callback() to generate RFC-compliant multipart bodies
- Added comprehensive tests for multipart format validation
- Added test for complete metadata handling (title, caption, description, alt_text)
- Ensured backward compatibility with existing functionality

Resolves Automattic#89
@galatanovidiu galatanovidiu self-requested a review August 19, 2025 07:38
Copy link
Contributor

@galatanovidiu galatanovidiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ref34t,

Thank you for this PR, but unfortunately, this does not work for me.
I'm getting a 500 error (No Content-Disposition supplied) both on the unit test and on Claude code.

The Issue:
The WordPress REST API attachment controller expects a Content-Disposition header when uploading files via raw data, but the current multipart form implementation isn't providing it in the expected format.

Technical Details:

  • The wp_upload_media_pre_callback creates multipart form data
  • WordPress's upload_from_data() method looks for content_disposition in the request headers
  • The multipart body structure isn't being parsed correctly by WordPress

The tests are failing with "Undefined array key 'content'" because the upload fails before returning the expected response structure.

Could you take a look at this? The multipart approach might need to be adjusted to work with WordPress's REST API expectations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp_upload_media sends invalid JSON to the WordPress instance

2 participants