I have a issue when i try send a file to Alfresco

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member

I have a issue when i try send a file to Alfresco

Jump to solution

Hi, 

I am try send a file to Alfresco.. My site web is Wordpress and i use a plugin Gravity Flow. I am creating a snippe pero this code is not work for me. This is me code 

public function process(){
// Do something here.
$entry = $this->get_entry();


$file = file_get_contents($entry['1']);
$url = 'http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children?alf_...';
$ch = curl_init();


curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
'Content-Disposition: form-data; filedata="Bilgais1.docx"',

] );
$result = curl_exec( $ch );
curl_close( $ch );
print_r( json_decode( $result ) );


return true;
}

 

I have this error 

{"error":{"errorKey":"Could not read content from HTTP request body: Unexpected character ('P' (code 80)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.BufferedReader@48162477; line: 1, column: 2]","statusCode":400,"briefSummary":"03020018 Could not read content from HTTP request body: Unexpected character ('P' (code 80)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.BufferedReader@48162477; line: 1, column: 2]","stackTrace":"Por motivos de seguridad, ya no se muestra el seguimiento de la pila, pero se guarda la propiedad para versiones anteriores.","descriptionURL":"https://api-explorer.alfresco.com"}}

I hope someone can help me. Thank you

1 Solution

Accepted Solutions
Highlighted
Community Manager
Community Manager

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @viperboys,

I've just created the following file upload.php. The code is as follows:

<?php
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://localhost/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?alf_ticket=TICKET_d8b9084ee020e4921687abfd0ce5a1c5ad4efd4f", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => array('name' => 'Eddie May Upload','filedata'=> new CURLFILE('/Users/emay/Desktop/Test/TEST TWO.docx')), CURLOPT_HTTPHEADER => array( "Authorization: Basic dGVzdDp0ZXN0" ), )); $response = curl_exec($curl); curl_close($curl); echo $response;

The response I got from calling the file (echo $response) is:

{"entry":{"isFile":true,"createdByUser":{"id":"admin","displayName":"Administrator"},
"modifiedAt":"2020-04-06T16:08:04.943+0000","nodeType":"cm:content",
"content":{"mimeType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"mimeTypeName":"Microsoft Word 2007","sizeInBytes":11760,"encoding":"UTF-8"},"parentId":"479ec2bc-fb73-4e93-9c44-b36d08ba0446",
"aspectNames":["cm:versionable","cm:titled","cm:auditable","cm:author"],"createdAt":"2020-04-06T16:08:04.943+0000",
"isFolder":false,"modifiedByUser":{"id":"admin","displayName":"Administrator"},
"name":"Eddie May Upload","id":"bf1ef22f-b759-4cc3-98ca-c68df911b132",
"properties":{"cm:versionLabel":"1.0","cm:author":"Eddie May",
"cm:versionType":"MAJOR"}}}

You have to make sure that your alf ticket is fresh - so you 1st need to call the login api to retrieve the ticket & add that to your curl url. Secondly, you probably need the destination noderef - here I'm just putting the file into my root.

 

HTH,

 

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!

View solution in original post

6 Replies
Highlighted
Community Manager
Community Manager

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @viperboys,

Can't say that I know exactly what's going wrong here, but 

Could not read content from HTTP request body: Unexpected character 

suggests that the file is not being sent properly. I'm not sure about the curlopt_httpheader values either - something there doesn't look right to me.

HTH,

 

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Active Member

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @EddieMay 

Thank you very much for answering.

My mistake is because I don't send the data well. I have tried it with CURL library and it doesn't work for me.

This is the error 

{"error":{"errorKey":"Required parameters are missing","statusCode":400,"briefSummary":"03030011 Required parameters are missing","stackTrace":"Por motivos de seguridad, ya no se muestra el seguimiento de la pila, pero se guarda la propiedad para versiones anteriores.","descriptionURL":"https://api-explorer.alfresco.com"}}

 

Can you tell me how is the correct way to send the files? Also useful is a use case with the CURL library for sending files. I have been guided by the code generated by POSTMAN but it has not worked for me either.

Highlighted
Community Manager
Community Manager

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @viperboys,

I've just created the following file upload.php. The code is as follows:

<?php
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://localhost/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?alf_ticket=TICKET_d8b9084ee020e4921687abfd0ce5a1c5ad4efd4f", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => array('name' => 'Eddie May Upload','filedata'=> new CURLFILE('/Users/emay/Desktop/Test/TEST TWO.docx')), CURLOPT_HTTPHEADER => array( "Authorization: Basic dGVzdDp0ZXN0" ), )); $response = curl_exec($curl); curl_close($curl); echo $response;

The response I got from calling the file (echo $response) is:

{"entry":{"isFile":true,"createdByUser":{"id":"admin","displayName":"Administrator"},
"modifiedAt":"2020-04-06T16:08:04.943+0000","nodeType":"cm:content",
"content":{"mimeType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"mimeTypeName":"Microsoft Word 2007","sizeInBytes":11760,"encoding":"UTF-8"},"parentId":"479ec2bc-fb73-4e93-9c44-b36d08ba0446",
"aspectNames":["cm:versionable","cm:titled","cm:auditable","cm:author"],"createdAt":"2020-04-06T16:08:04.943+0000",
"isFolder":false,"modifiedByUser":{"id":"admin","displayName":"Administrator"},
"name":"Eddie May Upload","id":"bf1ef22f-b759-4cc3-98ca-c68df911b132",
"properties":{"cm:versionLabel":"1.0","cm:author":"Eddie May",
"cm:versionType":"MAJOR"}}}

You have to make sure that your alf ticket is fresh - so you 1st need to call the login api to retrieve the ticket & add that to your curl url. Secondly, you probably need the destination noderef - here I'm just putting the file into my root.

 

HTH,

 

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!

View solution in original post

Highlighted
Active Member

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @EddieMay 

Thank you for you helping..

I am using yor code but this is not work for me.. It does not generate any error but it does not send the file. 

What can I do to send the file with CURL?

 

Highlighted
Community Manager
Community Manager

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @viperboys,

Sorry this is not working for you. If there's no log output, we probably have to go back to first principles.

1) Outside of Wordpress, can you use the api to log into Alfresco - this should return the alf_ticket? Create a simple php file that uses the api to get the alf ticket.

2) Once that's achieved, build on that to use curl to upload a document - again using a simple php file outside of Wordpress.

Once you've got that working, you'll then need to integrate into your Wordpress app. Wordpress has its own wrapper classes for curl up & downloading, like wp_remote_post(). You may need to use those rather than curl in my example - see this tutorial about calling APIs in Wordpress. Also (longshot) make sure there is nothing in wp-config.php, etc ., & also see if curl is enabled (phpinfo()).

At the moment I don't have Wordpress installed on my laptop to test the above in a Wordpress app.

HTH,

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Active Member

Re: I have a issue when i try send a file to Alfresco

Jump to solution

Hi @EddieMay 

Hello .. I already managed to solve my problem. I did it with Jquery and it works perfectly.

Thank you..