0

This curl command works:

curl -v -X POST https://subdomain.playvox.com/api/v1/files/upload?context=quality -H "Content-Type: multipart/form-data" -u [username]:[password] -F file=@c:\path\to\file.wav

But I am unable to perform the same thing in PowerShell using the Invoke-RestMethod cmdlet. Here's my attempt:

$file_contents = [System.IO.File]::ReadAllBytes($($file_path))

Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers @{ "Authorization" = "Basic $($playvox_base64_auth)" } -Body @{ file = $file_contents }

When run the API responds with invalid_param, "file" is required. However I confirmed the call to ReadAllBytes succeeds and gives back the raw file data. It seems like PowerShell is not sending the request body in the right format? I've looked at several other answers here and documentation online and nothing I found has worked.

Jackson Lenhart
  • 630
  • 3
  • 7
  • 19
  • What other solutions have you tried? There are a few different responses here that seem like good candiates. Have you tried using a .NET webclient class and using that to upload? https://stackoverflow.com/questions/38164723/uploading-file-to-http-via-powershell It looks like later versions of PowerShell have an -InFile parameter that you can use as well, if PS Version isn't a constraint for you: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.1 – Efie Mar 23 '21 at 15:28
  • @Efie I have tried -InFile but that doesn't work because the endpoint expects a form attribute called "file". Similarly for the UploadFile function on the webclient class, it only takes uri and file path, no way to emulate form attributes that I can see. – Jackson Lenhart Mar 23 '21 at 15:48

1 Answers1

0

Discovered there is a -Form parameter in Powershell 6 and later. I updated my powershell and used the following instead:

$file_contents = Get-Item $file_path

Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers @{ "Authorization" = "Basic $($playvox_base64_auth)" } -Form @{ file = $file_contents }

And it worked.

Jackson Lenhart
  • 630
  • 3
  • 7
  • 19