7

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

curl -i -F file=@file.txt 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?

1
  • I ended up downloading the curl executable for windows, and it worked wonders! Jun 3, 2013 at 16:25

4 Answers 4

9

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.

1
  • 2
    curl's -F is a multipart/form-data POST, so I guess most of this answer is then... not accurate Apr 22, 2013 at 6:39
5

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

curl.exe -i -F file=@file.txt 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

3
  • 1
    This is not what the OP asked for.
    – Warren P
    Mar 4, 2020 at 18:59
  • Sorry @WarrenP , but he asked for 'PowerShell equivalent for cURL command', that's curl.exe and the included link point to the exact answer to that question and fixes his problem
    – Troglo
    Mar 6, 2020 at 9:26
  • 2
    The link you linked to provides an accepted answer of Invoke-RestMethod:
    – Warren P
    Mar 19, 2020 at 16:30
0

To solve this, I download curl.exe for windows, and still used curl.

1
  • This sort of thing is better done as an edit to your question.
    – Warren P
    Mar 19, 2020 at 16:31
-2

After a couple hours of using fiddler to try to match curl, I gave up and just used this guy's function (See link below). Worked like a charm.

http://blog.majcica.com/2016/01/13/powershell-tips-and-tricks-multipartform-data-requests/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.