0

I need to upload files via commandline / powershell. It works fine with

curl -F "file=@test.txt" https://api.anonfiles.com/upload

However, curl does not exist on Windows Server 2016 and I do not want to tell my clients to set it up. So I am looking for a powershell alternative to accomplish this task. I tried various solutions, but none of them worked. What I tried so far:

(1)

$postParams = @{file='C:\users\user\testfile.txt'}; Invoke-WebRequest -Uri https://api.anonfiles.com/upload -Method POST -Body $postParams

(2)

Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -InFile "C:\users\user\testfile.txt"

(3)

$file=[io.file]::readallbytes('c:\users\user\testfile.txt')
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -Body "{ '@file':'"$file"'}"

None of it works. I canot believe it's so hard to replace an curl oneliner in powershell... The error in each case is a 400 http error, the request is wrong. How do I send the above mentioned curl-request equivalent in powershell? The site is https://anonfiles.com/docs/api

JoeDoe8877
  • 27
  • 1
  • Does this answer your question? [How to send multipart/form-data with PowerShell Invoke-RestMethod](https://stackoverflow.com/questions/22491129/how-to-send-multipart-form-data-with-powershell-invoke-restmethod) – PeterJ Apr 25 '22 at 11:39
  • BTW I confirmed the top voted answer works with that service, just do `$result = $client.PostAsync($url, $content)` and `Write-Output $result.Result.Content.ReadAsStringAsync()` to see the JSON output to get the destination URL – PeterJ Apr 25 '22 at 11:52

1 Answers1

-1

I gave up. It's not possible in Powershell. My App will just download curl.exe as standalone from now on, if it's not there.

JoeDoe8877
  • 27
  • 1