2

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.

2
  • 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? stackoverflow.com/questions/38164723/… 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: learn.microsoft.com/en-us/powershell/module/… Commented Mar 23, 2021 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. Commented Mar 23, 2021 at 15:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a note here: $file_contents is named misleadingly. It is rather a record describing the file with fields like the full path, the extension and the type. For Invoke-RestMethod, it provides enough info to access the content and the filename. The filename can be important to the receiving HTTP server, which may require it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.