使用net/http和POST请求发送图像的Ruby脚本

使用net/http和POST请求发送图像的Ruby脚本,ruby,post,net-http,Ruby,Post,Net Http,在一个ruby项目中,我试图将一个图像发送到一个服务器,以侦听POST请求。我想避免使用额外必要的宝石。我正在使用ruby 1.9.3和net/http 服务器不支持多端口,以下代码: require 'net/http' require "uri" image = File.open(image.path, 'r') url = URI("http://someserver/#{image_name}") req = Net::HTTP.new(url.host, url.port)

在一个ruby项目中,我试图将一个图像发送到一个服务器,以侦听POST请求。我想避免使用额外必要的宝石。我正在使用ruby 1.9.3和net/http 服务器不支持多端口,以下代码:

require 'net/http'  
require "uri"

image = File.open(image.path, 'r')

url = URI("http://someserver/#{image_name}")
req = Net::HTTP.new(url.host, url.port)
Net::HTTP.new(url.host, url.port)
headers = {"X-ClientId" => client_id, "X-ClientSecret" => client_secret, "Content-Type" => "image/jpeg"}
response, body = req.post(url.path, image, headers)
崩溃并显示以下错误消息:

http.rb:1932:in `send_request_with_body': undefined method `bytesize' for #<File:0x007ffdcd335270> (NoMethodError)
知道出了什么问题吗


提前感谢

为ruby脚本完成此操作-


对于rails,您可以使用“httmultipparty”。
通过这个-

以下是您可以执行的示例-

在控制器方法中,只需添加-

class SomeController <  ApplicationController
   def send_file
       response = Foo.post('/', :query => {
                  :file => File.new('abc.txt') 
       })
       # here Foo is class_name which is present in model/foo.rb
       response.code # it give response code success or failure.
   end
end

只需执行params[:file],即可在路径上获取文件。我认为这对您有帮助。

将文件读入字符串,不要尝试发送文件实例
post(url.path、image.read、headers)
最好不要打开文件,也不要关闭它
image=File.read('image\u path.jpg')
谢谢你的回答。我没有使用Rails,服务器不支持多部分上传。
class SomeController <  ApplicationController
   def send_file
       response = Foo.post('/', :query => {
                  :file => File.new('abc.txt') 
       })
       # here Foo is class_name which is present in model/foo.rb
       response.code # it give response code success or failure.
   end
end
require 'httmultiparty'
class Foo
   include HTTMultiParty
   base_uri 'my/path' #url where to send file 
end