Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

REST API Attachments

Phil Spence December 12, 2013

I can add an attachment to an existing issue from the command line using cURL as follows:

curl -D- -u testuser:testuser -X POST -H "X-Atlassian-Token: nocheck" -F "file=@test.txt" http://localhost:8090/rest/api/2/issue/TES-1/attachments

However, when I try to execute the same code from PHP using what I believe to be the exact same parameters, I recieve an "HTTP Status 415 - Unsupported Media Type". Here is the code:

<?php

$username = 'testuser';
$password = 'testuser';
$url = "http://localhost:8090/rest/api/2/issue/TES-1/attachments";
$data = array (
'file'=>'test.txt'
);

$ch = curl_init();
$headers = array(
"X-Atlassian-Token: nocheck",
'Content-Type: application/json'
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($ch);
$ch_error = curl_error($ch);

if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);

?>

TIA.

5 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Ruben Suarez Alvarez April 14, 2014

NOTE:

X-Atlassian-Token: nocheck

is not correct header value. It should be:

X-Atlassian-Token: no-check

Vesa Laakso March 10, 2015

For some reason our Jira 5.2 version only works with "nocheck" value. With "no-check" I still get "jira.utils.JIRAError: JiraError HTTP 404 text: XSRF check failed"

0 votes
Michael Bulger March 12, 2015

Phil - Did you ever find a solution?

 

kuldip naik October 15, 2018

POST /rest/api/2/issue/sdlcdevops-3238/attachments HTTP/1.1
Host: wim-jira.wellsfargo.com
X-Atlassian-Token: no-check
Authorization: Basic dTU3NjI3MjpGcmVtb250MTIz
Cache-Control: no-cache
Postman-Token: 94a3b1aa-454e-b976-b6e9-202ee407b753
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="agent.py"
Content-Type:


------WebKitFormBoundary7MA4YWxkTrZu0gW--

 

This has worked for me.

Like LostInMadness Forever likes this
0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2013
Phil Spence December 14, 2013

Progress, I think. Now I get the following error.

FileUploadException: the request was rejected because no multipart boundary was found

In the CLI, it seems like cURL takes care of setting the boundary -- Not sure if I have to set it manually.

0 votes
Phil Spence December 13, 2013

I tried removing 'Content-Type: application/json' but the result is the same.

0 votes
Zul NS _Atlassian_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 13, 2013

$headers = array(
"X-Atlassian-Token: nocheck",
'Content-Type: application/json'
);

Shouldn't the above be only X-Atlassian-Token: nocheck and not both?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events