Powershell upload a pdf file using boundary MIME to RightFax Web Service

763 Views Asked by At

I am trying to upload a pdf file into Right fax via the RightFax Web Api. I can do this via PostMan and I am able to send the attachment afterwards. When I try do upload via PowerShell an I send my attachment I only get the Object Name in the actual fax ex. System.Net.Http.StreamContent. Here is my borrowed powershell code:

Add-Type -AssemblyName System.Net.Http

$AuthURL = "http://" + $RESTAPIServer + "/RightFax/API"
$BaseURL = "http://" + $RESTAPIServer + "/RightFax/API/SendJobs"
$AttachURL = "http://" + $RESTAPIServer + "/RightFax/API/Attachments"
$Base = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))
$vCenterSessionURL = $BaseAuthURL 

$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))}
     $TARGET_FOLDER_PATH = "\\SomeFolderPath\"
     $TokenUri = "http://" + $RESTAPIServer + "/RightFax/API/Login?rememberMe={rememberMe}"

$Token = Invoke-RestMethod -Method GET -Headers $Header -Uri $TokenUri 

 Get-ChildItem $TARGET_FOLDER_PATH -Filter *.pdf  |
 Foreach-Object {

                  $TARGET_FILE_NAME = $_.Name
                  $LDA_TARGET_FILE_NAME = $TARGET_FOLDER_PATH + $TARGET_FILE_NAME 

                  $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                  $headers.Add("Authorization", "Basic $Base")


                  $fileName = $TARGET_FILE_NAME
                  $uri = $AttachURL
                  $filePath = $LDA_TARGET_FILE_NAME

                  $FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
                  $fileContent = [System.Net.Http.StreamContent]::new($FileStream)
                  $boundary = [System.Guid]::NewGuid().ToString()

                  $LF = "`r`n"
                  $bodyLines = (
                                "--$boundary",
                                "Content-Disposition: attachment; name=`"$fileName`"; filename=`"$filePath`"",
                                "Content-Type: application/octet-stream",
                                "Content-Transfer-Encoding: base64$LF",
                                $fileContent, 
                                "--$boundary--$LF"
                   ) -join $LF 

    $Attach = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines #-InFile $filePath
    write-host $Attach
    $FileStream.Dispose()
    $fileContent.Dispose()
}
2

There are 2 best solutions below

0
On

I'm setting up a different fax service (SRFax) that needs the attachments in base64.

$file = [IO.File]::ReadAllBytes("C:\Users\Path\To\File.pdf") 
$filebase64 = [System.Convert]::ToBase64String($file)
0
On

I figured it out. I needed to encode the file. For those of you who are here to actually get help and not here to play police for other users posts but neglect to add any actual positive helpful comments.... Here is the code for RightFax add attachments to a fax.

$AttachURL = "http://" + $RESTAPIServer + "/RightFax/API/Attachments"
$Base = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))
$vCenterSessionURL = $BaseAuthURL 

$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))}
     $TARGET_FOLDER_PATH = "\\SomeFolderPath\"
     $TokenUri = "http://" + $RESTAPIServer + "/RightFax/API/Login?rememberMe={rememberMe}"

$Token = Invoke-RestMethod -Method GET -Headers $Header -Uri $TokenUri 

 Get-ChildItem $TARGET_FOLDER_PATH -Filter *.pdf  |
 Foreach-Object {

                  $TARGET_FILE_NAME = $_.Name
                  $LDA_TARGET_FILE_NAME = $TARGET_FOLDER_PATH + $TARGET_FILE_NAME 

                  $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                  $headers.Add("Authorization", "Basic $Base")


                  $fileName = $TARGET_FILE_NAME
                  $uri = $AttachURL
                  $filePath = $LDA_TARGET_FILE_NAME

                 $fileRead = [IO.File]::ReadAllBytes($filePath)
                 $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
                 $fileEnc = $enc.GetString($fileRead)
                  $boundary = [System.Guid]::NewGuid().ToString()

                 $LF = "`r`n"
                 $bodyLines = (
                  "--$boundary",
                  "Content-Disposition: form-data; name=`"$fileName`"; filename=`"$ResultFilePath`"",   # filename= is optional
                  "Content-Type: application/pdf$LF",
                  $fileEnc,
     
                 "--$boundary--$LF"
                 ) -join $

       $Attach = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -TimeoutSec 20 -Body $bodyLines
      Write-host $Attach
}