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

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 Community Champion Apr 03, 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

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

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 Community Champion Apr 03, 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

thanks for your help

 I will try reaching dev community.

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,636 views 18 21
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you