PowerShell RestMethod POST API - Uploading a ZIP

57 views Asked by At

I recently upgraded my Automation Anywhere Control Panels and what used to work for a PowerShell API POST method to upload exported BOTs, no longer seems to work and I'm at a total loss.

Automation Anywhere defines two key pieces of information for Importing Bots via their API:

  • Officially, but dated, explains a process for the /v1/blm/import method and defines the request parameters.
  • Modern versions of the App use /v2/blm/import as outlined in this posted article. This is what I was and am attempting to use still.

I have a PowerShell script that takes an exported bot ZIP file and needs to POST that to Automation Anywhere. What I can't seem to figure out is if it's my script that is an issue or if Automation Anywhere secretly changed parameters. I'm hopeful someone here might know.

    #Read the zip file content
    $zipFileContent = [System.IO.File]::ReadAllBytes($FullPath)
     
    #Define the boundary for multipart/form-data
    $boundary = [System.Guid]::NewGuid().ToString()
    $LF = "`r`n"
     
    # Build the multipart/form-data body
    $multipartBody = "--$boundary$LF"
    $multipartBody += "Content-Disposition: form-data; name=`"upload`"; filename=`"$($FullPath | Split-Path -Leaf)`"$LF"
    $multipartBody += "Content-Type: application/zip$LF$LF"
    $multipartBody += [System.Text.Encoding]::UTF8.GetString($zipFileContent)
    $multipartBody += "$LF--$boundary--$LF"
     
    #Add actionIfExisting
    $multipartBody += "Content-Disposition: form-data; name=`"actionIfExisting`"$LF$LF"
    $multipartBody += "OVERWRITE$LF"
    $multipartBody += "--$boundary$LF"
     
    #Add publicWorkspace
    $multipartBody += "Content-Disposition: form-data; name=`"productVersionOption`"$LF$LF"
    $multipartBody += "current$LF"
    $multipartBody += "--$boundary--$LF"
     
    #Convert the body to a byte array
    $bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($multipartBody)
     
    $headers = @{
        "X-Authorization" = "$($response.token)"
    }
     
    Invoke-RestMethod -Uri $ImportURL -Method Post -Headers $headers -ContentType "multipart/form-data; boundary=$boundary" -Body $bodyBytes

$FullPath is a value containing the ZIP's local path.

PowerShell then constantly returns that my RestMethod has "Wrong request parameters".

Any guidance on what I might be doing wrong here?

Side note, I am aware that under PowerShell 7, Invoke-Restmethod does support a -Form mutlipart Content-Type, but I do not believe I want this for an API upload.

0

There are 0 answers