AnsweredAssumed Answered

REST - use a curfile object with json

Question asked by aliiiiii on Nov 29, 2017

Hi,

 

I'm working on POST request with alfresco & wordpress here is an example to create a node who si working :

$this->tempUrl = blablabla;
$request = new WP_Http();
$wp_request_headers = array(
'Authorization' => 'Basic YWRtaW46YWRtaW4='
);
$data = array(
'method' => 'POST',
'headers' => $wp_request_headers,
'body' => '{
"name":"test44242",
"nodeType":"cm:content"
}' );
$result = $request->request( $this->tempUrl, $data );

 

My next step is to upload a real file, using PHP i'm doing this and it's working :

$ch = curl_init($url);
$args = new CurlFile($filename,$type,$filename);
$data = array(
'nodeType'=>'cm:content',
'filedata'=>$args );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);

 

My issue is regarding the body of the first code i'm not able to include the curfile, if do something like that :

$args = new CurlFile('toto.txt','text/plain','toto.txt');
$filedetails = array(
'nodeType'=>'cm:content',
'filedata'=>$args
);
$data = array(
'method' => 'POST',
'headers' => $wp_request_headers,
'body' => json_encode($filedetails)
);

 

i receive this error : errorKey":"Could not read content from HTTP request body: Unrecognized field \"filedata\" (Class org.alfresco.rest.api.model.Node), not marked as ignorable\n at [Source: java.io.BufferedReader@6a1a6d13; line: 1, column: 38] (through reference chain: org.alfresco.rest.api.model.Node[\"filedata\"])","statusCode":400

 

UPDATE : Error 400 is normal we need to send the request with 'Content-Type' => 'multipart/form-data'

 

If i use 'Content-Type' => 'multipart/form-data' in the headers i receive : "errorKey":"No disk space available","statusCode":409,"briefSummary":"10290099 No disk space available" (there is space left i can upload using another script)

 

UPDATE : In my case the error was due to xml headers who was present in my $ticket (but invisible if i do an echo (i assume it was interpreted by the navigator) so problem solved

 

To send a file i need to put a curfile how can i do it ? do you see something wrong somewhere ?

 

thanks

Outcomes