Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:00
Show Gist options
  • Save laser/11303935 to your computer and use it in GitHub Desktop.
Save laser/11303935 to your computer and use it in GitHub Desktop.
RPC System Using HTTP
#!/usr/bin/env ruby
require 'net/http'
require 'securerandom'
require 'json'
uri = URI('http://localhost:4567/calc')
req = Net::HTTP::Post.new(uri.path)
req.body = {
'id' => SecureRandom.hex,
'jsonrpc' => '2.0',
'method' => 'add',
'params' => [1, 5.1]
}.to_json
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
puts "1+5.1=%.1f" % JSON.parse(res.body)['result'] # 1+5.1=6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment