ブラウザから投稿されたHMTLフォームのようなHTTPPOSTを実行したいと思います。具体的には、いくつかのテキストフィールドとファイルフィールドを投稿します。
テキストフィールドの投稿は簡単です。net/httprdocsに例がありますが、ファイルを一緒に投稿する方法がわかりません。
Net::HTTPは最良のアイデアのようには見えません。縁石はよさそうだ。
私はRestClientが好きです。マルチパート フォーム データのようなクールな機能で net/http をカプセル化します。
require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))
ストリーミングにも対応しています。
gem install rest-client
始めましょう。
Nick Sieger の multipart-post ライブラリについては、いくら言っても言い尽くせません。
これにより、Net::HTTP への直接のマルチパート ポスティングのサポートが追加され、自分のものとは異なる目標を持つ可能性のある境界や大きなライブラリについて手動で心配する必要がなくなります。
READMEからの使用方法の小さな例を次に示します。
require 'net/http/post/multipart'
url = URI.parse('http://www.example.com/upload')
File.open("./image.jpg") do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
"file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
ここでライブラリをチェックアウトできます: http://github.com/nicksieger/multipart-post
または次のようにインストールします。
$ sudo gem install multipart-post
SSL 経由で接続している場合は、次のように接続を開始する必要があります。
n = Net::HTTP.new(url.host, url.port)
n.use_ssl = true
# for debugging dev server
#n.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = n.start do |http|
curb
素晴らしい解決策のように見えますが、ニーズを満たさない場合は、Net::HTTP
. マルチパート フォーム ポストは、いくつかの追加ヘッダーを含む、慎重にフォーマットされた文字列です。マルチパート投稿を行う必要があるすべての Ruby プログラマーは、最終的に独自の小さなライブラリを作成するようです。なぜこの機能が組み込まれていないのか不思議に思います。多分それは...とにかく、あなたの読書の楽しみのために、私は先に進み、ここで私の解決策を提供します. このコードは、いくつかのブログで見つけた例に基づいていますが、リンクが見つからないことを残念に思います。だから私は自分自身のためにすべての信用を取る必要があると思います...
このために私が書いたモジュールにはString
、File
オブジェクトのハッシュからフォーム データとヘッダーを生成するためのパブリック クラスが 1 つ含まれています。たとえば、"title" という名前の文字列パラメーターと "document" という名前のファイル パラメーターを含むフォームを投稿する場合は、次のようにします。
#prepare the query
data, headers = Multipart::Post.prepare_query("title" => my_string, "document" => my_file)
次に、次のように通常の操作POST
を行いNet::HTTP
ます。
http = Net::HTTP.new(upload_uri.host, upload_uri.port)
res = http.start {|con| con.post(upload_uri.path, data, headers) }
または、他の方法で実行したいPOST
. ポイントは、Multipart
送信する必要があるデータとヘッダーを返すことです。以上です!シンプルですね。mime-types
Multipart モジュールのコードは次のとおりです ( gemが必要です)。
# Takes a hash of string and file parameters and returns a string of text
# formatted to be sent as a multipart form post.
#
# Author:: Cody Brimhall <mailto:brimhall@somuchwit.com>
# Created:: 22 Feb 2008
# License:: Distributed under the terms of the WTFPL (http://www.wtfpl.net/txt/copying/)
require 'rubygems'
require 'mime/types'
require 'cgi'
module Multipart
VERSION = "1.0.0"
# Formats a given hash as a multipart form post
# If a hash value responds to :string or :read messages, then it is
# interpreted as a file and processed accordingly; otherwise, it is assumed
# to be a string
class Post
# We have to pretend we're a web browser...
USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210"
CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }"
HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT }
def self.prepare_query(params)
fp = []
params.each do |k, v|
# Are we trying to make a file parameter?
if v.respond_to?(:path) and v.respond_to?(:read) then
fp.push(FileParam.new(k, v.path, v.read))
# We must be trying to make a regular parameter
else
fp.push(StringParam.new(k, v))
end
end
# Assemble the request body using the special multipart format
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
return query, HEADER
end
end
private
# Formats a basic string key/value pair for inclusion with a multipart post
class StringParam
attr_accessor :k, :v
def initialize(k, v)
@k = k
@v = v
end
def to_multipart
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
end
end
# Formats the contents of a file or string for inclusion with a multipart
# form post
class FileParam
attr_accessor :k, :filename, :content
def initialize(k, filename, content)
@k = k
@filename = filename
@content = content
end
def to_multipart
# If we can tell the possible mime-type from the filename, use the
# first in the list; otherwise, use "application/octet-stream"
mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
"Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
end
end
end
この投稿で利用可能な他の解決策を試した後の私の解決策は次のとおりです。TwitPicに写真をアップロードするために使用しています。
def upload(photo)
`curl -F media=@#{photo.path} -F username=#{@username} -F password=#{@password} -F message='#{photo.title}' http://twitpic.com/api/uploadAndPost`
end
OK、これは縁石を使った簡単な例です。
require 'yaml'
require 'curb'
# prepare post data
post_data = fields_hash.map { |k, v| Curl::PostField.content(k, v.to_s) }
post_data << Curl::PostField.file('file', '/path/to/file'),
# post
c = Curl::Easy.new('http://localhost:3000/foo')
c.multipart_form_post = true
c.http_post(post_data)
# print response
y [c.response_code, c.body_str]
NetHttp を使用したソリューションには、大きなファイルを投稿するときにファイル全体を最初にメモリにロードするという欠点があります。
少し遊んだ後、次の解決策を思いつきました。
class Multipart
def initialize( file_names )
@file_names = file_names
end
def post( to_url )
boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'
parts = []
streams = []
@file_names.each do |param_name, filepath|
pos = filepath.rindex('/')
filename = filepath[pos + 1, filepath.length - pos]
parts << StringPart.new ( "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
"Content-Type: video/x-msvideo\r\n\r\n")
stream = File.open(filepath, "rb")
streams << stream
parts << StreamPart.new (stream, File.size(filepath))
end
parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" )
post_stream = MultipartStream.new( parts )
url = URI.parse( to_url )
req = Net::HTTP::Post.new(url.path)
req.content_length = post_stream.size
req.content_type = 'multipart/form-data; boundary=' + boundary
req.body_stream = post_stream
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
streams.each do |stream|
stream.close();
end
res
end
end
class StreamPart
def initialize( stream, size )
@stream, @size = stream, size
end
def size
@size
end
def read ( offset, how_much )
@stream.read ( how_much )
end
end
class StringPart
def initialize ( str )
@str = str
end
def size
@str.length
end
def read ( offset, how_much )
@str[offset, how_much]
end
end
class MultipartStream
def initialize( parts )
@parts = parts
@part_no = 0;
@part_offset = 0;
end
def size
total = 0
@parts.each do |part|
total += part.size
end
total
end
def read ( how_much )
if @part_no >= @parts.size
return nil;
end
how_much_current_part = @parts[@part_no].size - @part_offset
how_much_current_part = if how_much_current_part > how_much
how_much
else
how_much_current_part
end
how_much_next_part = how_much - how_much_current_part
current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
if how_much_next_part > 0
@part_no += 1
@part_offset = 0
next_part = read ( how_much_next_part )
current_part + if next_part
next_part
else
''
end
else
@part_offset += how_much_current_part
current_part
end
end
end
可能な解決策の長いリストに追加するnick sieger のmultipart-postもあります。
私は同じ問題を抱えていました(jboss Webサーバーに投稿する必要があります)。コードでセッション変数を使用するとRubyがクラッシュする(ubuntu 8.10のruby 1.8.7)ことを除いて、縁石は私にとってはうまく機能します。
rest-client のドキュメントを調べたところ、マルチパート サポートの兆候が見つかりませんでした。上記の rest-client の例を試してみましたが、jboss は http 投稿がマルチパートではないと言いました。