PowerShell Invoke-RestMethod fails when uploading file attachment to JIRA issue

I’m trying to upload a text file to an existing JIRA issue using PowerShell’s Invoke-RestMethod with POST request but getting errors.

Here’s my PowerShell script (upload_file_to_jira.ps1):

param(
  [Parameter(Mandatory,HelpMessage='Issue Key')] [String]$issueKey
)

Write-Host "Processing issue: $issueKey"

$authToken = Get-Content ($PSScriptRoot + "\credentials\auth_token_base64.txt")

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$jiraApiUrl = "http://mycompany/jira/rest/api/2"

function Upload-FileToIssue(){
 param(
    [Parameter(Mandatory=$true)] [String]$issueKey
  )

  $fileToUpload = "report.txt"
  
  $response = Invoke-RestMethod -Method POST -ContentType "multipart/form-data" -InFile $fileToUpload -Uri "$jiraApiUrl/issue/$issueKey/attachments" -Headers @{Authorization=("Basic {0}" -f $authToken)}
  return $response
  Write-Host "Upload result: $response"
}

$uploadResult = Upload-FileToIssue -issueKey $issueKey
return $uploadResult

When I execute the script:

PS C:\MyScripts\jira> .\upload_file_to_jira.ps1 -issueKey PROJECT-12345

I get this error message:

Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At C:\MyScripts\jira\upload_file_to_jira.ps1:25 char:15
+   $response = Invoke-RestMethod -Method POST -InFile $fileToUpload -C ...
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

What could be causing this 404 error when trying to attach the file?

JIRA needs a specific header you’re missing: X-Atlassian-Token: no-check. Without it, JIRA’s CSRF protection blocks your request and throws a 404, even when your URL and auth are right. I hit this same issue automating deployment reports. Adding that CSRF header fixed it instantly. Also double-check your file path - make it absolute or properly resolve it relative to your script. -InFile needs the full path to work reliably. Just add "X-Atlassian-Token" = "no-check" to your headers hashtable with your Authorization header. This tells JIRA to skip token validation for uploads.

PowerShell’s -InFile parameter messes up multipart form data for JIRA’s attachment endpoint. I ran into this last year - Invoke-RestMethod doesn’t build the multipart boundary right. Don’t use -InFile with -ContentType. Instead, manually build the multipart form data or use the -Form parameter if you’re on PowerShell 6+. Also check if your JIRA instance actually supports that API version. Older setups sometimes don’t have the attachments endpoint enabled or need different auth scopes. That 404 might mean your auth token doesn’t have attachment permissions, not a URL problem.

you’re using the wrong content-type header. the JIRA attachment api is picky about this - just remove the ContentType parameter completely and let PowerShell handle it automatically. also double-check your JIRA URL path. sometimes it needs to be /rest/api/latest/ instead of /rest/api/2/

Been there with JIRA uploads. The 404 is usually a combo issue - wrong multipart formatting plus missing headers. PowerShell just isn’t great at this stuff.

You need the X-Atlassian-Token header like ZoeStar mentioned, but PowerShell’s multipart handling is still unreliable. The -InFile parameter works weird with JIRA’s API.

Honestly, after wrestling with similar PowerShell JIRA stuff for months, I switched everything to Latenode. Way cleaner.

Set up a workflow that handles JIRA attachments properly. Manages all the multipart encoding, CSRF tokens, and retry logic automatically. No more debugging PowerShell HTTP quirks or wondering if your headers are right.

You can trigger it from anywhere - scheduled tasks, webhooks, other tools. Much more reliable than scripts that break when PowerShell versions change or JIRA updates.

I use it for all our deployment reporting now. Files just upload without drama.

That 404 error happens because PowerShell’s Invoke-RestMethod can’t handle multipart file uploads to JIRA properly. The -InFile parameter just isn’t enough for JIRA’s attachment API.

I’ve hit this same wall before. JIRA’s attachment endpoint is super picky about multipart data formatting, and getting PowerShell to cooperate is a nightmare.

Skip the PowerShell headache and use Latenode instead. Build a workflow that connects to JIRA’s API correctly, handles file upload formatting automatically, and adds error handling plus retry logic.

With Latenode, you drag and drop the JIRA connector, point it at your file, and it handles all the multipart encoding. No more debugging HTTP requests or fighting PowerShell’s quirks.

You can also extend it later - auto-generate and attach reports, trigger uploads from other events, whatever. Much more flexible than scripts.