-1

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?

13
  • var_dump(curl_error($curl)) directly after curl_exec() should reveal more information about what's going wrong. Commented Feb 21, 2021 at 9: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. Commented Feb 21, 2021 at 9:44
  • Also, in case the connection succeeds, but the API returns an error, add CURLOPT_FAILONERROR => true to your options array to catch that. Commented Feb 21, 2021 at 9:48
  • 1
    @SynnKo It will be helpful to export and post the curl request that works from Postman. Commented Feb 21, 2021 at 10:58
  • 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. Commented Feb 21, 2021 at 11:10

1 Answer 1

1
+50

Se the content type to:

"Content-Type: multipart/form-data"
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.