How to send a file via XML RPC

Does anyone have some sample code showing how to send a file via XML
RPC?

Sending string values is easy … like the below example…

server = XMLRPC::Client.new2(“http://rpc.technorati.com/rpc/ping”)
result = server.call(“weblogUpdates.ping”, “Copenhagen.rb”,
"http://www.copenhagenrb.dk/

… but how would one send a file?

Etienne

On Apr 15, 2008, at 6:56 PM, e deleflie wrote:

Does anyone have some sample code showing how to send a file via XML
RPC?

I can’t remember where I first saw this.

$ cat client.rb
#!/usr/bin/ruby

require ‘xmlrpc/client’
require ‘base64’

data = IO.read(’/tmp/test.jpg’)
encoded_file = Base64.encode64(data)

server = XMLRPC::Client.new3({‘host’=>‘localhost’, ‘port’=>8888,
‘timeout’=>30})

if server.call(“put_file”, encoded_file)
puts “ok”
end
puts result

$ cat server.rb
#!/usr/bin/ruby

$SAFE = 1

require “xmlrpc/server”
require ‘base64’

s = XMLRPC::Server.new(8888)
s.add_handler(“put_file”) { |encoded_file|
Dir.chdir(’/tmp/test’)
File.open(‘test.jpg’, ‘w+’) { |file|
file.puts( Base64.decode64(encoded_file) )
}
true
}

s.serve

bingo! … … just needed to encode the binary data into base64
before sending it (and do the reverse at the server end).

thanks
Etienne

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs