1

I'm backing up my hard drive on the cloud with a filehost. This filehost provides an API to do it but i just can't get it works on powershell.

On their API website (http://code.google.com/p/filecloud/wiki/Uploading) they claim that "We upload the file via HTTP POST request" so I use my trusty POST_Request function:

function POST_Request ($url,$parameters) {
$client = New-Object System.Net.WebClient 
$contents = [Text.Encoding]::UTF8.GetString(($client.UploadValues($url,$parameters) ))
return $contents
}

Since they website claim that they need 2 parameters to be sent in order to upload (a kind of id of my account and the file) i cannot use UploadFile in a System.Net.WebClient object.

In summary, my question is: How can I pass a parameter (the id) AND the file to be uploaded via POST? (maybe a trick with UploadFile will do it)

Many thanks in advance!

1 Answer 1

1

Normally those are put in a NameValueCollection IIRC and the NameValueCollection instance is passed as the "values" parameter to UploadValues(). You're using $parameters. Is that passed in to you or do you create it? Either way, if it is a NameValueCollection, use the Add method to add the name/value pairs for the required parameters.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep, that's exactly what $parameters is: $parameters = New-Object Collections.Specialized.NameValueCollection; $parameters["Filedata"] = $file; $parameters["id"]= $MyID
After I call POST_Request I get the exception: Exception calling "UploadValues" with "2" argument(s): "The remote server returned an error: (415) Unsupported Media Type."
Have you set the content-type header on the request? What is the server expecting in terms of file type? Is it XML, plain text, JSON? I think you'll need to set the content-type header appropriately (application/xml, text/plain, application/json).
Chris N I've tried with the full path of the file. @Keith Hill I'm not sure... but maybe it's JSON just like what the format of the response is by default. Sorry for not providing this detail, I'm not an expert but you can take a look at this. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.