It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Post File as an Attachment Using Confluence Rest API with PHP curl

Hello All,

I have trying for quite awhile to post a file as an attachment using the rest api with php curl. I have seen similar questions out there, and I tried to emulate those who said they had success, but it just does not seem to work for me. I would greatly appreciate and welcome any suggestions.

 

My code:

$ch = curl_init();
$file = 'C:\path\path\path\path.txt';

if (function_exists('curl_file_create')) {
     $cFile = curl_file_create($file);
} else { 
     $cFile = '@' . realpath($file);
}
$postData = array('file_contents' => $cFile);
$headers = array();
$headers[] = "X-Atlassian-Token: no-check"; 


curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/31391748/child/attachment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "****" . ":" . "****");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$result = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);
curl_close ($ch);

 

I noticed I am getting a HTTP code of 400 when $info prints, which I know means it is a bad request. I am not sure what could potentially be causing this. Again, any help will be much appreciated!

2 answers

1 accepted

0 votes
Answer accepted
Thomas Deiler Community Leader Feb 02, 2020

Dear @Coline_Macorol ,

as written in the API document:

The name of the multipart/form-data parameter that contains attachments must be "file"

Replace

$postData = array('file_contents' => $cFile);

with 

$postData = array('file' => $cFile);

So long

Thomas

Hi Thomas,

Thanks so much for the quick response! I'm very happy to report that it worked! 

 

Cheers,

Coline Macorol

Hi all,

I'm unfortunately back to say the php curl is once again not working. Thanks to php curl error handling, it appears the problem is cURL error (26): Failed to open/read local data from file/application.

I know this means it is unable to open the file it is pointing to. However, I don't know why it isn't, especially since it worked before and I haven't changed anything but the file. Could it be the file path? Or is it something else I'm not seeing?

 

Thanks,

Coline Macorol

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

Lessons and Learnings: Six Months of Working Remote [Discussion]

Hey there, folks! For most of us, the past six months- yes, you read that right- have been a journey. More people than ever before have pivoted to working remotely, and navigating being on-scre...

5,345 views 4 6
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you