Closed (fixed)
Project:
Services
Version:
6.x-3.0-beta2
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
5 Feb 2011 at 10:27 UTC
Updated:
9 Feb 2011 at 09:55 UTC
I'm trying to use the file resource with the REST server on services v6.x-3.0-beta2 to create a file.
While RESTis working for content that can be expressed as JSON (e.g. nodes), I can't seem to figure out how exactly to create a valid drupal_http_request to create a file using the file resource.
Could anyone point me to a description or a working example of how this is done ?
Thx !
Comments
Comment #1
lubrifix commentedThis seems to work, should anyone ever wonder....
Some php code to generate the JSON :
function buildFile($filePath)
{
$data = array(
'file' => array(
'file' => base64_encode(file_get_contents($filePath)),
'filepath' => 'sites/default/files/images/'.$filePath,
'filename' => $filePath,
'filesize' => filesize($filePath),
'timestamp' => time()
)
);
$returnData = json_encode($data);
return $returnData;
}
And for the actual request, the following function could accept the data that was returned from the buildFile function. The function uses durpal_http_request, but curl could be used instead.
function restFileRequest($url, $op, $data)
{
$request_headers = array("content-type" => "application/json");
$request = drupal_http_request($url.$op, $request_headers, 'POST', $data);
if ($request->error)
print ("error doing http request");
else
return $request->data;
}
Comment #2
gdd