0

I'm trying to add an attachment to a page using the REST API and PHP, but I can't get it to work. I got it to work with Postman, no problem, but the PHP code it spits out doesn't seem to be working, and it doesn't even give out an error message.

This is the code I've been using:

<?php


$curl = curl_init();

$restApiUrl = 'http://localhost:8090/rest/api/content/{pageId}/child/attachment/';
$filePath = 'C:/Users/{user}/Desktop/example.png';
$auth = '{auth}';

curl_setopt_array($curl, array(
    CURLOPT_URL => $restApiUrl,
    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('file' => new CURLFILE($filePath)),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: image/png;charset=UTF-8',
        'X-Atlassian-Token: no-check',
        'Authorization: Basic ' . $auth
    ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

The values inside curly brackets (like {auth}) are just placeholders.

Nothing seems to happen with this, no error message, no attachment, nothing... All previous requests I've created in Postman (like creating new pages) usually worked in PHP right away, but not file uploads. Does anyone know what I'm doing wrong here?

Synn Ko
  • 137
  • 1
  • 15
  • `var_dump(curl_error($curl))` directly after `curl_exec()` should reveal more information about what's going wrong. – Kontrollfreak Feb 21 '21 at 09:25
  • `image/png;charset=UTF-8` makes no sense. And it's not clear if you want to POST the image directly or via a multipart form. – Olivier Feb 21 '21 at 09:44
  • Also, in case the connection succeeds, but the API returns an error, add `CURLOPT_FAILONERROR => true` to your options array to catch that. – Kontrollfreak Feb 21 '21 at 09:48
  • Are you really using `{pageId}`,`{user}` and `{auth}` in your code? – BeS Feb 21 '21 at 10:29
  • @BeS No, those are just placeholders. Edited the question to clarify it. – Synn Ko Feb 21 '21 at 10:49
  • @Olivier I wanted to submit the image directly without multipart, but if the Confluence API allows me to upload multiple files through multipart, I would use multipart instead. I imagine you say the content type part doesn't make sense because of the UTF-8 part? That's probably a left-over from another API call. Let me know if that's what you meant and I'll edit the question. – Synn Ko Feb 21 '21 at 10:54
  • @Kontrollfreak Thank you, I'll give those a try as soon as I'm back home. – Synn Ko Feb 21 '21 at 10:55
  • 1
    @SynnKo It will be helpful to export and post the curl request that works from Postman. – BeS Feb 21 '21 at 10:58
  • Yes, a charset makes no sense with an image. On top of that, `image/png` makes no sense with `CURLOPT_POSTFIELDS`. – Olivier Feb 21 '21 at 11:02
  • 1
    @Olivier I'm sorry but just saying "it makes no sense" doesn't really help me, and I already feel enough like a dumbass as it is... If I'm not supposed to use "post fields" with a file, what am I supposed to use instead? The above code was generated from Postman, so I assumed it was correct with its structure. – Synn Ko Feb 21 '21 at 11:10
  • More precisely, using an array with `CURLOPT_POSTFIELDS` is incorrect here. According to [this](https://stackoverflow.com/questions/871431/raw-post-using-curl-in-php), you should assign the image contents directly: `CURLOPT_POSTFIELDS => file_get_contents($filePath)`. – Olivier Feb 21 '21 at 11:28
  • Check this answer: https://stackoverflow.com/a/15200804/6087422 – adampweb Feb 21 '21 at 13:41
  • @Kontrollfreak Apologies for the late reply, I just couldn't get to it in time... bounty might get lost, but whatever. After some changes I got the error "The requested URL returned error: 415". I don't know if that helps, I'm still trying some stuff out. – Synn Ko Feb 27 '21 at 12:14

1 Answers1

1

Se the content type to:

"Content-Type: multipart/form-data"
getl0st
  • 342
  • 1
  • 10
  • Simple answers like this always remind me I'll never be great at programming. I tried so many damn things to make the code work, just not THIS... and it ends up working... thank you for helping me get a load off my mind. – Synn Ko Feb 27 '21 at 13:26
  • 1
    You're welcome, had the same problem a few weeks ago, there is so much that we don't know about HTTP requests that it amazes me – getl0st Feb 27 '21 at 19:27