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 ?

Matilda
  • 1,708
  • 3
  • 25
  • 33

1 Answers1

1

Yes you can do it by "httmultiparty"
In your controller method just add -

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

In model just make any file, let foo.rb and add following code -

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

You will get file on your path by just doing params[:file] . I think this help you.

Sandip Karanjekar
  • 850
  • 1
  • 6
  • 23