在ruby项目中,我正在尝试将图像发送到侦听POSTS请求的服务器。我想避免使用额外的必要宝石。我使用ruby 1.9.3和net / http 服务器和以下代码不支持Multiport:
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)
在bash中取得以下成功:
curl -i -X POST --data-binary @./output.jpg -H 'X-ClientId: ##..##' -H 'X-ClientSecret: ##..##' http:/someserver/output.jpg
知道出了什么问题吗?
提前致谢
答案 0 :(得分:0)
浏览ruby脚本 - http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
或强>
对于rails,你可以使用'httmultiparty'
仔细阅读 - https://github.com/jwagener/httmultiparty
以下是您可以做的示例 -
在你的控制器方法中只需添加 -
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
在模型中只需创建任何文件,让foo.rb添加以下代码 -
require 'httmultiparty'
class Foo
include HTTMultiParty
base_uri 'my/path' #url where to send file
end
只需执行params [:file],您就可以获得路径中的文件。我认为这有助于你。