0

I am trying to do a multipart post with parameters in ruby securely using https. All the examples I have seen are only http without parameters in addition to the file. However, I can't seem to modify them to get them to work with https and additional parameters (or find documentation showing a good example). How can I do a multipart POST using HTTPS in ruby with parameters? I have tried modify the code from Nick Sieger as shown below but to no avail. Where do I add parameters that I need to pass in in JSON format in addition to the file?

# push file to rest service
    url = URI.parse('https://some.url.test/rs/test')
    File.open(tm.created_file_name) do |txt|
      req = Net::HTTP::Post::Multipart.new url.path,
                                           'file' => UploadIO.new(txt, 'text/plain', tm.created_file_name)
      n = Net::HTTP.new(url.host, url.port)
      n.use_ssl = true
      p req.body_stream
      res = n.start do |http|
        response = http.request(req)
        p response.body
      end
    end
Brent
  • 303
  • 3
  • 11
  • if you don't absolutely need to use Net::HTTP, I'd just go with Faraday: https://stackoverflow.com/questions/16725195/upload-files-using-faraday – Reck Sep 15 '15 at 18:43
  • possible duplicate of [Ruby: How to post a file via HTTP as multipart/form-data?](http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data) – Alexey Shein Sep 15 '15 at 20:06
  • This question builds on that one...but is different because I need both secure and with additional parameters in JSON – Brent Sep 16 '15 at 12:39

1 Answers1

0

I figured out to do a multipart form post using https and parameters. Here is the code:

require 'rest-client'
url = 'https://some.url/rs/FileUploadForm'
@res = RestClient.post url, {:multipart=>true,:tieBreakerOptions=>1,
                                 :myFileName=>'file.txt',
                                 :myFile=>File.new('data/file.txt','r')}
response = JSON.parse(@res)
Brent
  • 303
  • 3
  • 11