Confluence Server and uploading a file using Powershell

I am working on uploading a file to our Confluence Server. To be more specific, I created a temp dev site and I can upload files just fine using Powershell 7+ but cannot using Powershell 5.1. Our internal apps only have 5.1 so I am working on the work around to use 5.1

In the PS7 script, I simply used the “-forms” switch and it worked fine on Invoke-WebRequest. For PS5.1, I had to come up with something else.

$URL                = ""
$pageId             = "1234567890"
$filePath           = 'c:\temp\test.txt'
$fileName           = "test.txt"
$fileDestination = $URL + "rest/api/content/$pageId/child/attachment/"

$fileBytes   = [System.IO.File]::ReadAllBytes($filePath);
$fileEnc     = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary    = [System.Guid]::NewGuid().ToString();
$LF = "`r`n";

# login credentials
$username   = "username"
$password   = ConvertTo-SecureString "verylongpassword" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)

# headers
$headers = @{ "X-Atlassian-Token" = "no-check" }

$bodyLines = (
    "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
    "Content-Type: application/octet-stream$LF",
) -join $LF

Invoke-RestMethod -Uri $fileDestination `
                  -Method Post `
                  -Credential $credential `
                  -Headers $headers `
                  -ContentType "multipart/form-data; boundary=`"$boundary`"" `
                  -Body $bodyLines

The error I get.

  "statusCode": 403,
  "data": {
    "authorized": false,
    "valid": true,
    "allowedInReadOnlyMode": true,
    "errors": [],
    "successful": false
  "message": "User not permitted to create attachments for content: ContentId{id=1234567890}",
  "reason": "Forbidden"