Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ruby rest_客户端,未识别mimetype_Ruby On Rails_Rest Client - Fatal编程技术网

Ruby on rails ruby rest_客户端,未识别mimetype

Ruby on rails ruby rest_客户端,未识别mimetype,ruby-on-rails,rest-client,Ruby On Rails,Rest Client,我有一个RestClient,它执行以下调用: RestClient::Request.new( :method => "post", :url => "http://myservice.com/call.json", :payload => {'document[data]' => File.new(e.image.path, 'rb')}, :headers => { :accept => :json, :co

我有一个RestClient,它执行以下调用:

RestClient::Request.new(
    :method => "post",
    :url => "http://myservice.com/call.json",       
    :payload => {'document[data]' => File.new(e.image.path, 'rb')},
    :headers => { :accept => :json, :content_type => :json}
    ).execute
我实现了一个接收rest调用的小测试服务器,因为我不是rest服务的作者。 在下面,您可以找到请求的实际内容以及它应该是什么。 我的问题是:在哪里指定附加文件的mime类型

它是什么:

#<ActionDispatch::Http::UploadedFile:0x0... @original_filename="x.pdf", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"document[data]\"; filename=\"x.pdf\"\r\nContent-Type: text/plain\r\n", @tempfile=#> # 它应该是什么:

#<ActionDispatch::Http::UploadedFile:0x0... @original_filename="x.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"document[data]\"; filename=\"x.pdf\"\r\nContent-Type: application/pdf\r\n", @tempfile=#> #
奇怪的是,RestClient应该能够基于文件扩展名获取mime类型

无论如何,如果我没记错的话,就没有办法为基于文件的多部分段传递mime类型,但是Payload类中有代码查找内容类型。这可能已经被修复,但在解决此问题之前,此黑客应该可以工作

module MyModule
   def content_type
     "application/pdf"
   end
end

f = File.new(e.image.path, 'rb')}
f.extend(MyModule)

RestClient::Request.new(
   :method => "post",
   :url => "http://myservice.com/call.json",       
   :payload => {'document[data]' => f},
   :headers => { :accept => :json, :content_type => :json}
   ).execute

这个答案帮了我很大的忙:向上投票是不够的!!!另一方面,我必须检查一个非棘手的解决方案。我想我从您的解决方案开始,检查RestClient在哪里调用您的content\u类型方法