API - Jira Download Attachment - PowerShell

I’m trying to download Jira issue attachments via PowerShell. All is working great on my personal cloud instance of Jira, but when I attempt to download on my client’s server Jira instance, I get a corrupted file. I believe it has something to do with credentials, but I am successfully executing many API calls against the Project/Issues with the Personal Access Token (PAT), which is used as the bearer token for authorization.

Any help would be awesome!

Here is my PowerShell scirpt:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Bearer [PersonalAccessToken]")

$response = Invoke-RestMethod '[URL]/secure/attachment/20202/KSD GE 6109 10 11 Email.pdf' -Method 'GET' -OutFile '[FolderPath]\KSD GE 6109 10 11 Email.pdf' -Headers $headers
$response | ConvertTo-Json

Hello @BrianKeller

The thing you’re asking to GET is a PDF file, not JSON, so you need a -ContentType of application/pdf in the header, not application/json

Although I’ve not done it in a while in PowerShell, I remember omitting the -ContentType from the header and just accepting the raw data stream. Invoke-RestMethod then just piped it straight into the -OutFile location. Try that too.

1 Like

Thanks @sunnyape . I’m on it! Let’s see what we get!

@sunnyape I omitted the -ContentType and you are right…that was not needed. I still get a corrupt file as the output. I think this has to do with the user rights in Jira. Looking into that…