Question:
I am able to send HTTP POST a text file to my server using the following cURL command:
1 2 |
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:
1 2 3 4 5 |
$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?
Answer:
I think it should be something like this:
1 2 3 |
$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.