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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With