Synn Ko
Synn Ko

Reputation: 87

How to add attachments to a page using the REST API and PHP?

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?

Upvotes: -1

Views: 1293

Answers (1)

getl0st
getl0st

Reputation: 342

Se the content type to:

"Content-Type: multipart/form-data"

Upvotes: 1

Related Questions