0
$FreeUploadServer = Invoke-RestMethod -Uri "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver"
Invoke-RestMethod -Uri "http://rs$FreeUploadServer.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload&login=43533592&password=password&filecontent=C:\libs\test.txt" 

I have no clue why that isnt working, its something to do with the filecontent parameter but i have read the entire documentation about the API and i cant figure it out.

API Documentation

  • 2
    In cases like this, I highly recommend the Fiddler web debugging proxy tool. It can show you what PowerShell is actually sending and what the server's response is. http://www.fiddler2.com/fiddler2/ – Keith Hill Sep 03 '12 at 17:29
  • ive tried, im going crazy lol. I normally use Charles to debug but i even decided to give fiddler a shot and i cant figure it out. –  Sep 03 '12 at 17:39
  • What response is the server returning? – Keith Hill Sep 03 '12 at 17:40
  • ERROR: No files transmitted. (f269d341) Its a rapid share specific error, while you here i have a question for you, should i be using Invoke-WebRequest instead of Invoke-RestMethod? Whats the difference anyway, you could use ((Invoke-WebRequest "someendpoint").ParsedHTML.Body | ConvertFrom-JSON) to get the same results as Invoke-RestMethod? –  Sep 03 '12 at 17:44

2 Answers2

4

Make sure you use the -Method Post parameter/arg combo on the second Invoke-RestMethod call. Also, take a look at this SO response to a similar question about using CURL. After looking at the question and answer, it appears that RapidShare requires filling in POST form fields. In this case you need to specify the -Body parameter. I think you'll use a PowerShell hashtable where each entry corresponds to a form field name/value pair. Looks like the one field needed is the filecontent field. Presumably the value is the file's contents.

Also, when you use POST, you'll have to convert the GET query parameters to POST form fields e.g.:

$url = "http://rs$FreeUploadServer.rapidshare.com/cgi-bin/rsapi.cgi"
$fields = @{sub='upload';login='43533592';password='password';filename='test.txt';
            filecontent=([IO.File]::ReadAllText('c:\libs\test.txt'))}

Invoke-RestMethod -Uri $url -Body $fields -Method Post

If [IO.File]::ReadAlltext() doesn't cut it perhaps try [IO.File]::ReadAllBytes() instead.

Community
  • 1
  • 1
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • when i do that i get a different error by the way it says: ERROR: Post without content. –  Sep 03 '12 at 17:48
  • i tried this: Invoke-RestMethod "http://rs$FreeUploadServer.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload&login=43533592&password=password&filecontent=C:\libs\test.txt" -Body @{filecontent="C:\libs\test.txt"} -Method Post it failed but this time with subroutine failed, would there be a reason to remove filecontent from the URL? –  Sep 03 '12 at 17:58
  • I think this is close. The kicker is how to actually transmit the content of the file. – Keith Hill Sep 03 '12 at 18:02
  • yeah anyway, i shall mark you as the answer cause to tell you the truth i had been playing with -Body, but forgot that in posh you define name/value pairs with a hashtable, im sure ill get it adventually. –  Sep 03 '12 at 18:05
  • FINALLY. i found it, im going to answer my own question with the solution :D –  Sep 03 '12 at 19:13
  • @TaylorGibb So how is your solution *any* different than what I posted? For your function's $File parameter, is that the name of the file or the file's content? And really, considering I pointed in you in the direction of `-Method Post` and moving the Get query parameters down into the form fields, you down vote my answer. Seems a bit harsh unless the answer has no redeeming value. :-) – Keith Hill Sep 03 '12 at 21:17
  • I never down voted your answer :| in fact i will mark you as correct. I never saw your updated answer until i came to post, but since you method would work as well i guess i can mark you as the answer. –  Sep 04 '12 at 10:20
0

Turns out there is a tiny section in the API Documentation that says if you use the POST method all parameters need to be passed in the body. I wrote a function that uploads to RapidShare in PowerShell, i hope this helps people in the future cause this was VERY annoying.

function Upload-RapidShare([string]$File,[string]$Name)
{
     $FreeUploadServer = Invoke-RestMethod -Uri "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver"
     Invoke-RestMethod "http://rs$FreeUploadServer.rapidshare.com/cgi-bin/rsapi.cgi" -Body  "sub=upload&login=USERNAME&password=PASSWORD&filename=$Name&filecontent=$File" -Method Post
}
  • 1
    You realize that specifying the body by string like that is exactly equivalent to using a hashtable for the form fields. If you look in Fiddler when specifying a hashtable for the Body parameter, the HttpRequest's body (as captured by Fiddler) is `filename=test.txt&sub=upload&password=password&login=43533592&filecontent=...` – Keith Hill Sep 03 '12 at 21:04