Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Send attachments using Jira oauth1.0 PHP bitbucket Jira library

sandeep
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 25, 2020

Hi I am using the  https://bitbucket.org/atlassianlabs/atlassian-oauth-examples/src/master/php/ for oAuth process and requests.
This is working fine and  I am able to use the post and get requests.
But in this  I am not sure how to build the attachment request.

The post request is something like which is working fine.


$headers_array = array("Content-Type"=> "application/json");
$post_data = @file_get_contents('php://input');
$priorities = $oauth->getClient('oauth_token',
'oauth_secet')
->post($endpoint,$headers_array,$post_data)
->send()->json();
Please suggest how to build a request to use jira attachment api
I am trying something like this for attachment

$headers_array = array("X-Atlassian-Token" => "nocheck",
"Content-Type" => "multipart/form-data"
);
$cfile = curl_file_create("test.txt");
$data = json_encode(array('file' => $cfile));
$priorities = $oauth->getClient('oauth_token',
'oauth_secet')
->post("rest/api/2/issue/GC-2200/attachments",$headers_array,$data);

but this is not working and throwing error.Please suggest.

Thanks

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 2, 2020

Hi Sandeep,

I see that you are trying to make REST API calls to Jira using the php library of the oauth 1.0 library.  Sorry to hear this is not working.  However I'd like to try to learn more about what that error message is.

Referring to the Jira Cloud REST API reference for that endpoint I can see that this uses a slightly different header of

 --header 'Accept: application/json'

for that curl example.  I see you listed this as Jira Server, but I fear that our Jira Server documentation is not being as explicit in this regard when it probably should be.  In short, I think you need to use that header when making this request instead of the Content-Type one.

Sometimes Jira can be very picky about the headers included in such requests.  But I'm not confident that will resolve this entirely.  Taking another look at the PHP example in Cloud, it appears to be using the unirest library in order to be able to capture the response Jira provides. 

// This code sample uses the 'Unirest' library:
// http://unirest.io/php.html
Unirest\Request::auth('email@example.com', '');

$headers = array(
  'Accept' => 'application/json'
);

$response = Unirest\Request::post(
  '/rest/api/2/issue/{issueIdOrKey}/attachments',
  $headers
);

var_dump($response)

Granted that basic auth, and your authorization will be different in OAuth.  But I would recommend that we have some way to capture the response here, unirest is one way to do that.  I am not sure if you're seeing the specific error Jira is throwing at your current request or not.  That should help us further understand what Jira does not understand about the request sent to it.

Let me know the results.

Andy

sandeep
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 4, 2020

@Andy HeinzerIt is working fine with the basic auth , but the issue is with the oauth, All other apis are working fine through oauth, the problem is only occurring in the case of attachments. Is there any sample code for attachment api you can provide for php library.

Thanks

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 5, 2020

Check out this post from JeromeFlores https://community.atlassian.com/t5/Jira-questions/Re-Upload-URL-Attachment-via-API-on-JIRA-PHP/qaq-p/44830/comment-id/19019#M19019  He posted some PHP code he used to perform attachments to Jira:

/**
* CURL post request to the JIRA REST API for Attachments only
* 
* @param $issueKey - Jira Issue Key
* @param $file - url of the file or Uploaded temp path
* @param $name - name of the file(used on uploaded file) default is null
* @return json Attachment self
*/
private static function postJSONFile( $issueKey, $file, $name = null )
{ 

$file_name = $name ? $name : time().'-'.basename($file); //creating file name

$path = public_path( 'temp/'.$file_name ); //creating temp path

file_put_contents( $path , file_get_contents($file)); //saving file in a temp path
$cfile = new \CURLFile( $path ); // initiating CURLFile for preparing the upload
$cfile->setPostFilename( $file_name ); //setting the file name

$data = array('file'=>$cfile); //creating array for Curl Post Fields
$ch = curl_init(); // initiating curl
//setting the headers
$headers = array(
'X-Atlassian-Token: nocheck',
'Content-Type: multipart/form-data'
);
//setting curl option on array
curl_setopt_array( $ch, array(
CURLOPT_URL => config( 'jira.url' ) . '/rest/api/latest/issue/' . $issueKey . '/attachments',
CURLOPT_USERPWD => config( 'jira.username' ) . ':' . config( 'jira.password' ),
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_VERBOSE => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
) );
//executing curl
$response = curl_exec( $ch );
curl_close( $ch ); //closing curl
unlink( $path ); //deleting the file
return $response; //returning values
}

But again this is a basic auth example.

I also ran across https://stackoverflow.com/questions/28079632/adding-attachment-to-jira-via-api Which seems to also provide an example of how to make this work.  Seems that they noted that PHP might not be explicitly listing the path of the file which Jira's REST API might be expecting.  

Let me know if either of these are helpful, I will seek to update this REST API document to provide some more detailed examples for different languages.

Andy

Suggest an answer

Log in or Sign up to answer