Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#35388 closed enhancement (wontfix)

Add support to wp_remote_request to support multipart/form-data

Reported by: pbearne's profile pbearne Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: HTTP API Keywords:
Focuses: Cc:

Description

I had to push a file using 'multipart/form-data' to an API and wanted to use internal the internal remote request code instead of a 3rd party library.

I was able to with this code but its a bit messy so I would like to suggest that we add methods to wp_remote_request to support this e.g. PUT_MUTLI and POST_MULTI and payload is passed in the body arg if as an array or object, then we split the content into the elements of the multipart allowing the defaults to be overwritten

I am posting this without a patch to make sure I patch along these lines would accepted?

Thoughts?


$local_file = $_FILES['resume']['tmp_name'];
                // wp_remote_request way

                //https://github.com/jeckman/wpgplus/blob/master/gplus.php#L554
                $boundary = md5( time() . $ext );
                $payload  = '';
                $payload .= '--' . $boundary;
                $payload .= "\r\n";
                $payload .= 'Content-Disposition: form-data; name="photo_upload_file_name"; filename="' . $_FILES['resume']['name'] . '"' . "\r\n";
                $payload .= 'Content-Type: ' . $format . '\r\n'; // If you      know the mime-type
                $payload .= 'Content-Transfer-Encoding: binary' . "\r\n";
                $payload .= "\r\n";
                $payload .= file_get_contents( $local_file );
                $payload .= "\r\n";
                $payload .= '--' . $boundary . '--';
                $payload .= "\r\n\r\n";

                $args = array(
                        'method'  => 'PUT',
                        'headers' => array(
                                'accept'       => 'application/json', // The API returns JSON
                                'content-type' => 'multipart/form-data;boundary=' . $boundary, // Set content type to multipart/form-data
                        ),
                        'body'    => $payload,
                );

                $url      = add_query_arg(
                        array(
                                
                                'externalID'  => 'Portfolio',
                                'fileType'    => 'SAMPLE',
                        ), self::$url . '/file/'
                );
                $response = wp_remote_request( $url, $args );

Change History (2)

#1 @chriscct7
9 years ago

  • Version trunk deleted

#2 @dd32Lead Developer
8 years ago

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

Since WordPress has switched to Requests enhancements such as this should be opened against it instead.

I'm marking this as wontfix however due to no traction as of yet.

Note: See TracTickets for help on using tickets.