Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#24156 closed defect (bug) (wontfix)

If you submit file through wp curl it doesnt send boundary params for file mutlipart

Reported by: samnani's profile samnani Owned by:
Milestone: Priority: normal
Severity: major Version: 3.5.1
Component: HTTP API Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

If you submit file through wp curl it doesnt send boundary params for file multipart, because in class-http.php at line 174 $r['body'] array is converted to string:

$r['body'] = http_build_query( $r['body'], null, '&' );

And curl wants array to send file boundary to rest api , in that case it fails.
ex.

$headers['Content-type'] = 'multipart/form-data';   
$args['file'] = '@/pathtofile';
$request = new WP_Http;
$result = $request->request($url,$args);

Let me know if you have questions

Attachments (1)

send_filedata_curl.diff (1.2 KB) - added by samnani 12 years ago.
Patch for cURL send-file functionality for WP_HTTP transports

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
12 years ago

  • Description modified (diff)

#2 @dd32
12 years ago

To put it bluntly, if you want to use cURL functionalities that WP_HTTP doesn't support, you should probably be using cURL directly.

The other WP_HTTP transports do not support the cURL send-file functionality, and you can't be sure that WP_HTTP will be using cURL on all servers. The enhancement here would be to support sending files via multipart in WP_HTTP.

@samnani
12 years ago

Patch for cURL send-file functionality for WP_HTTP transports

#3 @samnani
12 years ago

  • Cc samnani added

Hi I have rewritten few line of code and it worked, here i am adding diff file, where i can ask for the review or submit a patch for wordpress code?

#4 @SergeyBiryukov
12 years ago

  • Keywords has-patch added; needs-patch removed

#5 @dd32
12 years ago

  • Keywords 2nd-opinion added

#6 @dd32
12 years ago

  • Keywords 2nd-opinion removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

WP_HTTP is designed to be a abstraction layer which allows you to use any available method of connecting to a remote site.

If you want to use curl functionality, you need to use curl directly.

If someone requires it, please open a new ticket to add generic File uploading capability to WP_HTTP instead.

#7 @markoheijnen
12 years ago

I'm doing it like this and it seems to work fine:

$body      = file_get_contents( $filepath );
$mime_type = '';

if ( extension_loaded( 'fileinfo' ) ) {
	$finfo = new finfo;
	$mime_type = $finfo->file( $filepath, FILEINFO_MIME );
}
elseif ( function_exists('mime_content_type') ) {
	$mime_type = mime_content_type( $filepath );
}

$args = array(
	'method'  => 'PUT',
	'headers' => array(
		'Content-Type'           => $mime_type,
		'X-HTTP-Method-Override' => 'PUT'
	),
	'timeout' => 30,
	'body'    => $body,
);

$response      = wp_remote_request( $url, $args );
Note: See TracTickets for help on using tickets.