Using restclient with multipart posts
Asked Answered
M

3

6

I'm using restclient for a multipart form to send data to a restful web service (it's Panda video encoding service). The trick though, is that the file I am passing into restclient (Technoweenie branch) is coming from my own form that a user submits.

So, lets walk through this. A user posts a file to my rails app. In my controller, it receives the file from params[:file]. I then want to pass params[:file] down to Panda using RestClient.

The error I'm getting is on the Panda server follows. I noticed that the file param in the stack trace is in a string as well (which I assume is Panda turning into a string for a nicer stacktrace).

 ~ Started request handling: Wed Aug 12 18:05:15 +0000 2009
 ~ Params: {"format"=>"html", "multipart"=>"true", "account_key"=>"SECURE_KEY", "action"=>"upload", "id"=>"SECURE_ID", "controller"=>"videos", "file"=>"#<File:0xcf02ca4>"}
 ~ 9bfb1750-6998-012c-4509-12313900b0f6: (500 returned to client) InternalServerErrorcan't convert nil into String
/var/local/www/panda/app/models/video.rb:246:in `extname'
/var/local/www/panda/app/models/video.rb:246:in `initial_processing'
/var/local/www/panda/app/controllers/videos.rb:79:in `upload'
Mafalda answered 12/8, 2009 at 18:23 Comment(1)
Can you say how you figure out this problem?Forerunner
P
1

I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.

A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.

What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]

Potable answered 13/8, 2009 at 2:54 Comment(0)
A
1

Confirm that your restclient action can save locally first. If the action cannot save locally, then you will have a better idea where to look while trouble shooting.

Abednego answered 12/8, 2009 at 20:9 Comment(0)
P
1

I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.

A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.

What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]

Potable answered 13/8, 2009 at 2:54 Comment(0)
B
0

Looks like the problem is with rest-client's posting of the file, check out an alternative method for posting like curb.

Lots of examples for posting multipart form data on this question: Ruby: How to post a file via HTTP as multipart/form-data?

Butterfly answered 12/8, 2009 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.