How to attach files to jira issues using JIRA REST module in powershell

Rajiv Sharma April 3, 2017

I am trying to attach a txt file from my local computer to Jira issue using Invoke-Restmethod and i am getting following error

Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type.
At C:\scripts\jira4.ps1:30 char:15
+ $newstatus = Invoke-RestMethod -Method POST -Headers $headers -ContentType "tex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 I am using following command to run it

$newstatus = Invoke-RestMethod -Method POST -Headers $headers -ContentType "text/html;charset=UTF-8" -InFile $ticketfile -uri "$target/rest/api/2/issue/$key/attachments"

No matter what i change for "Contenttype" i still get the same error.

1 answer

1 vote
Maarten Cautreels
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.
April 3, 2017

Hi Rajiv,

This seems to be a development related question. With the launch of the new community, the developer community has been split from this community. You will probably have more luck finding an answer if you as it there.

To not leave you empty handed I think you should change the ContentType header to multipart/form-data as that's what the JIRA REST documentation says.

Best,

Maarten

Rajiv Sharma April 3, 2017

Thanks Maarten

when i change contenttype to Multipart/form-data i get the following error:

Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At C:\scripts\jira4.ps1:30 char:15
+ $newstatus = Invoke-RestMethod -Method POST -Headers $headers -ContentType "mul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Rajiv Sharma April 3, 2017

Even when i use curl command i get following response but nothing attached to my jira task

PS C:\Users\rajiv.sharma> curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@c:\scripts\myfile.txt" http://jira/rest/api/2/issue/key/attachments
HTTP/1.0 302 Found
Location: https://jira/rest/api/2/issue/key/attachments
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

Maarten Cautreels
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.
April 3, 2017

Hi Rajiv,

Unfortunately I don't have too much experience with the REST API. 

According to the docs: Returned if the requested issue is not found, the user does not have permission to view it, or if the attachments exceeds the maximum configured attachment size.

A 302 unfortunately is not documented but could be returned by either the apache, nginx or in your case BigIP, your load balancer, in front of JIRA. 

So maybe look into that or head over to the Developer Community as it contains more technical experienced Atlassian Users and Developers. :-)

Sorry I couldn't help.

Best,

Maarten

Rajiv Sharma April 3, 2017

thanks for your help

 I will try reaching dev community.

 

Suggest an answer

Log in or Sign up to answer