Trouble uploading text file to JIRA ticket using PowerShell's Invoke-RestMethod

Hey folks, I’m trying to add a small text file to an existing JIRA ticket using PowerShell. I’m using the Invoke-RestMethod command with a POST request, but I’m running into some issues.

Here’s a snippet of what I’m working with:

function Add-JiraAttachment {
  param($ticketId)

  $file = 'data.txt'
  $apiUrl = 'https://myjira.com/api/v2'
  $auth = Get-Content 'auth.txt'

  $response = Invoke-RestMethod -Method POST -Uri "$apiUrl/issue/$ticketId/attachments" -Headers @{Authorization="Basic $auth"} -ContentType 'multipart/form-data' -InFile $file

  Write-Host "Response: $response"
}

Add-JiraAttachment -ticketId 'PROJ-123'

When I run this, I get a 404 error. Any ideas what I might be doing wrong? I’ve double-checked the JIRA ID and API URL, but I’m still stuck. Thanks for any help!

yo, i had this prob too. the trick is to use the -Form param instead of -InFile. also, make sure ur api endpoint is correct. try this:

$response = Invoke-RestMethod -Method POST -Uri “$apiUrl/rest/api/2/issue/$ticketId/attachments” -Headers @{Authorization=“Basic $auth”} -Form @{file=Get-Item $file}

this worked 4 me. lmk if u need more help!

I’ve encountered similar issues when working with JIRA’s REST API in PowerShell. From my experience, the problem might be with how you’re handling the multipart/form-data content type. Invoke-RestMethod doesn’t natively support this format well.

Here’s what worked for me:

  1. Use the -Form parameter instead of -InFile.
  2. Create a custom boundary string.
  3. Construct the body manually using byte arrays.

Here’s a modified version of your function that should work:

function Add-JiraAttachment {
  param($ticketId)

  $file = 'data.txt'
  $apiUrl = 'https://myjira.com/api/v2'
  $auth = Get-Content 'auth.txt'

  $boundary = [System.Guid]::NewGuid().ToString()
  $LF = "`r`n"

  $bodyLines = (
    "--$boundary",
    "Content-Disposition: form-data; name=\"file\"; filename=\"$file\"",
    "Content-Type: application/octet-stream$LF",
    [System.IO.File]::ReadAllBytes($file),
    "--$boundary--$LF"
  )

  $body = $bodyLines | ForEach-Object { $_ }

  $response = Invoke-RestMethod -Method POST -Uri "$apiUrl/issue/$ticketId/attachments" -Headers @{Authorization="Basic $auth"} -ContentType "multipart/form-data; boundary=$boundary" -Body $body

  Write-Host "Response: $response"
}

This approach should resolve the 404 error you’re encountering. Let me know if you need any clarification!

I’ve worked extensively with JIRA’s API, and I can offer some insights. The issue you’re facing is likely related to authentication or the API endpoint structure. First, ensure your auth token is correctly formatted and up-to-date. JIRA’s API is sensitive to token expiration.

Secondly, double-check the API endpoint. Some JIRA instances use a slightly different structure. Try modifying your URL to:

“$apiUrl/rest/api/2/issue/$ticketId/attachments”

Additionally, make sure your account has the necessary permissions to add attachments to the specific ticket.

If these don’t resolve the issue, you might want to look into using a dedicated JIRA PowerShell module like PSJira. It handles many of these intricacies automatically, making API interactions much smoother.