Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby send JSON request

How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send. Do I have to have javascript send the form?

Or can I use the net/http class in ruby?

With header - content type = json and body the json object?

like image 927
Andy Avatar asked Jan 08 '10 00:01

Andy


1 Answers

uri = URI('https://myapp.com/api/v1/resource') req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') req.body = {param1: 'some value', param2: 'some other value'}.to_json res = Net::HTTP.start(uri.hostname, uri.port) do |http|   http.request(req) end 
like image 52
CarelZA Avatar answered Oct 21 '22 02:10

CarelZA