Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell equivalent of curl HTTP POST for file transfer

I'm currently uploading a file via an HTTP post with a call like this:

curl --verbose --data-binary @C:\Projects\TestUploadFiles\TestFile1.csv "http://client.abc.com/submit?username=UserX&password=PasswordHere&app=test1&replacejob=TestNewJob&startjob=n"

This works fine. However, I actually have about 3000 files daily to upload - every file in my directory. I was thinking about just writing out a batch file that has multiple cURL commands, one for each file. But this would leave me with the overhead of opening and closing the connection once for each file, right?

So, I am considering PowerShell. I'm not familiar with it, but I believe that I might be able to use WebRequest for this purpose.

Would this be a good option? Any code sample pointers?

like image 274
Sylvia Avatar asked Dec 14 '11 14:12

Sylvia


1 Answers

In theory the following...

curl --verbose --data-binary @C:\Projects\TestUploadFiles\TestFile1.csv "http://client.abc.com/submit?username=UserX&password=PasswordHere&app=test1&replacejob=TestNewJob&startjob=n"

It could be replaced by using System.Net.WebClient.UploadFile. For example, to upload all CSV files in the current directory:

$wc = new-object System.Net.WebClient
ls *.csv | foreach {
    $wc.UploadFile( 'http://client.abc.com/submit?username=UserX&password=PasswordHere&app=test1&replacejob=TestNewJob&startjob=n', $_.FullName )
}
like image 146
Scott Saad Avatar answered Nov 08 '22 16:11

Scott Saad