本文介绍了多部分 POST Ruby HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 https 安全地使用 ruby​​ 中的参数进行多部分发布.我看到的所有例子都是除了文件之外没有参数的http.但是,我似乎无法修改它们以使它们与 https 和其他参数一起使用(或找到显示一个很好示例的文档).如何在带参数的 ruby​​ 中使用 HTTPS 执行多部分 POST?我尝试修改 Nick Sieger 的代码,如下所示,但无济于事.除了文件之外,我在哪里添加需要以JSON格式传入的参数?

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

推荐答案

我想出了使用 https 和参数进行多部分表单发布的方法.代码如下:

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)

这篇关于多部分 POST Ruby HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:53