Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell equivalent for cURL command uploading file

I am able to send HTTP POST a text file to my server using the following cURL command:

curl -i -F [email protected] http://www.website.com/put_file

The server is expecting a file from $_FILES['file'].

I have the following so far but it is not working:

$url = http://www.website.com/put_file
$file = "c:\file.txt"
$wc = new-object System.Net.WebClient
$wc.UploadFile($url, $file.FullName)

it returns 0, and it did not upload to the server

How can I send the file as $_FILES['file']? Also, how can I see the response from the server?

like image 259
Ivan Santos Avatar asked Apr 22 '13 00:04

Ivan Santos


2 Answers

I think it should be something like this:

$body = "file=$(get-content file.txt -raw)"
Invoke-RestMethod -uri http://www.websetite.com/put_file -method POST -body $body

Note: this does require PowerShell V3. Also, you might need to specify the -ContentType parameter as "application/x-www-form-urlencoded". I recommend getting Fiddler2 and use it to inspect the request in both the curl and Inovke-RestMethod cases to help get the irm parameters correct. This is assuming the file contents are fairly small and non-binary. If not, you probably need to go to content type multipart/form-data.

like image 93
Keith Hill Avatar answered Sep 20 '22 12:09

Keith Hill


In Powershell you can use curl.exe instead of curl (yes, are different commands: http://get-cmd.com/?p=5181)

curl.exe -i -F [email protected] http://www.website.com/put_file

Or you can use Invoke-RestMethod from powershell 3

More details about Invoke-RestMethod from powershell 3 at: https://superuser.com/questions/344927/powershell-equivalent-of-curl

like image 27
Troglo Avatar answered Sep 18 '22 12:09

Troglo