2

I'm trying to do a Net::HTTP:POST::Multiport and send some text in a file format to an API.

I get the data from my database, and I don't want to create physical files, I want to create a file on the fly with the data and send it to the API. Right now I have

request = Net::HTTP::Post::Multipart.new("my/path", 
                                           { "file" => UploadIO.new(file, "text/plain", file.path), 
                                             "merge" => false, 
                                             "ignore_missing" => false, 
                                             "label" => "", 
                                             "low_priority" => false })

Here file is supposed to be a file object I read from the disk or an IO according to http://rubydoc.info/gems/multipart-post/1.1.0/UploadIO#initialize-instance_method, any idea how I can just create a JSON IO from a bunch of strings, without having to create a file and write to it?

Also any idea how I can do this via httmultiparty ?

4

1 回答 1

1

是的,您可以通过“httmultiparty”
在您的控制器方法中添加 -

class SomeController <  ApplicationController
   def send_file
       response = Foo.post('/', :query => {
                  :file => File.new('abc.txt') # Generate your file here
       })
       # here Foo is class_name which is present in model/foo.rb
       response.code # it give response code success or failure.
   end
end

在模型中,只需创建任何文件,让 foo.rb 并添加以下代码 -

require 'httmultiparty'
class Foo
   include HTTMultiParty
   base_uri 'my/path' #url where to send file 
end  

您只需执行 . 即可获得路径上的文件params[:file]。我认为这对你有帮助。

于 2012-12-05T11:26:42.710 回答