Ruby 带机载的多部分POST,故障为“1”;缺少起始边界“;

Ruby 带机载的多部分POST,故障为“1”;缺少起始边界“;,ruby,rspec,rest-client,airborne,Ruby,Rspec,Rest Client,Airborne,使用rest客户端已经有一段时间了,但对机载设备来说是新的。我遇到的一个障碍是,我似乎无法获得文件上传的多部分文章。我收到了一个错误的请求“缺少起始边界”,我的空降岗位如下所示: before :all do fyle = File.new("e:/some-file.jpg", 'rb') body = {:profile => "KonflictME", :file => fyle, :multipart => true} post "/uplo

使用rest客户端已经有一段时间了,但对机载设备来说是新的。我遇到的一个障碍是,我似乎无法获得文件上传的多部分文章。我收到了一个错误的请求“缺少起始边界”,我的空降岗位如下所示:

  before :all do
    fyle = File.new("e:/some-file.jpg", 'rb')
    body = {:profile => "KonflictME", :file => fyle, :multipart => true}
    post "/uploads", body, { :content_type => 'multipart/form-data' }
  end
fyle = File.new("e:/some-file.jpg, 'rb')
resp = RestClient.post base_path_api << "uploads",
  {:profile => "KonflictME", :file => fyle, :multipart => true},
  {:authorization => "myTokenString}
我使用rest客户端的工作示例如下所示:

  before :all do
    fyle = File.new("e:/some-file.jpg", 'rb')
    body = {:profile => "KonflictME", :file => fyle, :multipart => true}
    post "/uploads", body, { :content_type => 'multipart/form-data' }
  end
fyle = File.new("e:/some-file.jpg, 'rb')
resp = RestClient.post base_path_api << "uploads",
  {:profile => "KonflictME", :file => fyle, :multipart => true},
  {:authorization => "myTokenString}
fyle=File.new(“e:/some-File.jpg,'rb')
resp=RestClient.post base_path_api“KonflictME”,:file=>fyle,:multipart=>true},
{:authorization=>“myTokenString}
两者之间没有太大区别,因为rest客户端是在引擎罩下用于机载的。对于机载设备,基本url和头在配置(spec_helper.rb)中设置


有没有其他人尝试过使用Airbord的多部件post?上传只是我的测试套件的一小部分,我有一个非机载解决方案(直接使用rest客户端进行这些测试),但我想让团队的其他成员保持简单。

机载框架基于restclient,在源代码中lib/airbar/rest_client_requester.rb中有一行:

request\u body=request\u body.to\u json if options[:body]。是(散列)

这意味着您定义的每个主体都将被转换为json,并作为以下行中的参数:

RestClient.send(方法、获取url(url)、请求正文、标题)

但在RestEclipse中,无论您想发布JSON还是上传文件,都不需要考虑正文的模式。如果要上载文件,可以将上述代码更改为:

request\u body=request\u body.to\u json if options[:body]。是否为a?(哈希)和&options[:body]。to\u json[“file”]。nil?

检查您定义的主体是否包含文件。我在最近的项目中做了这个改变


希望这能对您有所帮助。

机载框架基于restclient,在源代码lib/airbar/rest_client_requester.rb中有一行:

request\u body=request\u body.to\u json if options[:body]。是(散列)

这意味着您定义的每个主体都将被转换为json,并作为以下行中的参数:

RestClient.send(方法、获取url(url)、请求正文、标题)

但在RestEclipse中,无论您想发布JSON还是上传文件,都不需要考虑正文的模式。如果要上载文件,可以将上述代码更改为:

request\u body=request\u body.to\u json if options[:body]。是否为a?(哈希)和&options[:body]。to\u json[“file”]。nil?

检查您定义的主体是否包含文件。我在最近的项目中做了这个改变

希望这会对您有所帮助。

您可以使用RestClient(Airbored的一个依赖项)使这变得相当简单。用散列法正常写出参数:

params = {
              other_param: 'Some param',
              file: File.new(Rails.root.join('spec', 'fixtures', 'sample.csv'), 'r')
          }
      }

multipart = RestClient::Payload::Multipart.new(params)
post endpoint, params: multipart.read, headers: multipart.headers
您可以使用RestClient(airboard的一个依赖项)使这相当容易。用散列法正常写出参数:

params = {
              other_param: 'Some param',
              file: File.new(Rails.root.join('spec', 'fixtures', 'sample.csv'), 'r')
          }
      }

multipart = RestClient::Payload::Multipart.new(params)
post endpoint, params: multipart.read, headers: multipart.headers