Make WordPress Core

Opened 7 years ago

Closed 5 years ago

#41774 closed defect (bug) (invalid)

REST-API: Media endpoint (POST): Unable to upload files

Reported by: tiger42's profile tiger42 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.1
Component: REST API Keywords: dev-feedback
Focuses: rest-api Cc:

Description

I have found a possible bug in the file "wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php", which prevents me from uploading media files via the "media" REST endpoint. Every time I try to upload a file, I get the following response:

{
   "code": "rest_upload_unknown_error",
   "message": "File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.",
   "data": {"status": 500}
}

The problem seems to be in line 719:

$file = wp_handle_upload( $files['file'], $overrides );

After some debugging, I have found out that the array in the $files variable does not have the format that seems to be expected. After changing the line to the following code, the file upload works for me without problems:

$file = reset( $files );
$file = wp_handle_upload( $file, $overrides );

Change History (3)

#1 @rmccue
7 years ago

  • Keywords dev-feedback added

Hi @tiger42, and thanks for the report. I don't think this is a bug in the code, although we might be missing some documentation here.

How are you sending the file to the REST API? There are two ways you can pass files to the API:

  1. Send the file's data in the request body (raw mode). This also requires setting the Content-Disposition header to pass the filename.
  2. Send the file in multipart mode (upload mode). This needs to be passed via the file parameter in a multipart/form-data request.

It appears that you're using upload mode here, but not setting the parameter name to file.

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


5 years ago

#3 @kadamwhite
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Closing due to lack of feedback, as this does not appear to have been a recurring issue with the API itself.

Note: See TracTickets for help on using tickets.