Attaching files with Jira REST API returns empty array

I am creating a script in PHP that will attach a file to an existing Jira issue. When running the script, all that is returned is an empty array. I am wondering what is wrong with my code:

$cfile = curl_file_create($_SERVER['DOCUMENT_ROOT'].'/test.png','image/png','test.png');

$data1 = array('test.png' => $cfile);

$url1 = 'http://myserver.com/rest/api/2/issue/TP-55/attachments';
$ch1 = curl_init();
$headers1 = array(
    'Content-Type: multipart/form-data',
    'X-Atlassian-Token: nocheck'
);

curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_VERBOSE, 1);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch1, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_URL, $url1);

$username = 'myusername';
$password = 'mypassword';
curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");

$result1 = curl_exec($ch1);
$ch_error1 = curl_error($ch1);


if ($ch_error1) {
    echo "cURL Error: $ch_error1";
} else {
    echo $result1;
}

curl_close($ch1);

      

With this code, I am just trying to upload a test file that is already on the server. What do I need to change to complete a successful file upload?

+3


source to share


1 answer


Add one or more attachments to the problem. This resource is awaiting a multipage entry. The multi-format / formatted media type is defined in RFC 1867. Most client libraries have classes that make it easier to work with multi-page messages. For example, in Java, the Apache HTTP Components library provides MultiPartEntity, which makes it easy to send a multipart POST. To protect against XSRF attacks, since this method accepts multipart / form-data, it has XSRF protection on it. This means you have to send the X-Atlassian-Token: nocheck header with the request, otherwise it will be blocked. The name of the multipart / form-data parameter containing attachments should be "file" A simple example of uploading a file called "myfile.txt" for the REST-123 release: curl -D- -u admin: admin -X POST -H "X- Atlassian-Token:nocheck "-F"file=@myfile.txt "... / rest / api / 2 / issue / TEST-123 / attachments



so the data you pass must contain a field name file =@test.png. Example above

+1


source







All Articles