How to send a file via XML RPC

L

lists

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
 
E

e deleflie

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

thanks
Etienne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top