views:

93

answers:

1

Hi,

I need to make a HTTP POST call to a Hudson CI server from CakePHP. The call is parametrized and contains a few key/value pairs. Some of those are in fact files which need to be uploaded.

Since I'm using CakePHP, I'd rather use the HttpSocket class which comes with the framework rather then try and write my own cURL based implementation.

So far, the call looks like this:

$result = $http->post($apiCall, $params, $auth);

$apiCall being the URI of the Hudson REST API. $params being an array of parameters which go with the POST call. $auth containing a user/pass for Basic Auth which configured with this is instance of Hudson.

I'm a bit puzzled though: what would I need to do to also included files in my $params array?

We are running Hudson v1.371 which should - as far as I've gathered - support file uploads coming from a parametrized build call.

Thanks!

A: 

I don't know if the HttpSocket class handles multipart Http requests. But you can create it manually. I've done this in my CakePHP GData Plugin that includes functionality for uploading videos to YouTube. The save method in the YouTubeVideo model creates a multipart Http request with the first part containing an XML document with meta data about the video and the second part is the binary contents of the video file being uploaded:

// The boundary string is used to identify the different parts of a
// multipart http request
$boundaryString = 'Next_Part_' . String::uuid();

// Build the multipart body of the http request
$body = "--$boundaryString\r\n";
$body.= "Content-Type: application/atom+xml; charset=UTF-8\r\n";
$body.= "\r\n";
$body.= $doc->saveXML()."\r\n";
$body.= "--$boundaryString\r\n";
$body.= "Content-Type: {$data[$this->alias]['file']['type']}\r\n";
$body.= "Content-Transfer-Encoding: binary\r\n";
$body.= "\r\n";
$body.= file_get_contents($data[$this->alias]['file']['tmp_name'])."\r\n";
$body.= "--$boundaryString--\r\n";

$this->request = array(
  'method' => 'POST',
  'uri' => array(
    'host' => 'uploads.gdata.youtube.com',
    'path' => '/feeds/api/users/default/uploads',
  ),
  'header' => array(
    'Content-Type' => 'multipart/related; boundary="' . $boundaryString . '"',
    'Slug' => $data[$this->alias]['file']['name']
  ),
  'auth' => array(
    'method' => 'OAuth',
  ),
  'body' => $body,
);

This might get you there.

neilcrookes