WordPress.org

Make WordPress Core

Opened 12 months ago

Closed 9 months ago

Last modified 9 months ago

#24156 closed defect (bug) (wontfix)

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

Reported by: 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 months ago.
Patch for cURL send-file functionality for WP_HTTP transports

Download all attachments as: .zip

Change History (8)

comment:1 SergeyBiryukov12 months ago

  • Description modified (diff)

comment:2 dd3212 months 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.

samnani12 months ago

Patch for cURL send-file functionality for WP_HTTP transports

comment:3 samnani12 months 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?

comment:4 SergeyBiryukov12 months ago

  • Keywords has-patch added; needs-patch removed

comment:5 dd329 months ago

  • Keywords 2nd-opinion added

comment:6 dd329 months 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.

comment:7 markoheijnen9 months 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.