Ruby sends a JSON request

How to send a JSON request to ruby? I have a JSON object, but I don’t think I can just do .send . Should I have javascript to submit the form?

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

With title - content type = json and body - json object?

+61
json ruby
Jan 08
source share
10 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 
+53
Jan 21 '14 at 15:56
source share
 require 'net/http' require 'json' def create_agent uri = URI('http://api.nsa.gov:1337/agent') http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json') req.body = {name: 'John Doe', role: 'agent'}.to_json res = http.request(req) puts "response #{res.body}" rescue => e puts "failed #{e}" end 
+32
May 15 '14 at 11:26
source share

HTTParty makes this a little easier, I think (and works with nested json, etc. that don't seem to work in the other examples I've seen.

 require 'httparty' HTTParty.post("http://localhost:3000/api/v1/users", body: {user: {email: 'user1@example.com', password: 'secret'}}).body 
+15
Jan 24 '13 at 0:39
source share

A simple json POST request example for those who need it is even simpler than what Tom gets attached to:

 require 'net/http' uri = URI.parse("http://www.example.com/search.json") response = Net::HTTP.post_form(uri, {"search" => "Berlin"}) 
+6
Aug 28 2018-12-12T00:
source share

Assuming you just want to quickly and dirtyly convert the hash to json, send json to the remote host to test the API and parse the ruby ​​response, this is probably the fastest way without involving additional stones:

 JSON.load 'curl -H 'Content-Type:application/json' -H 'Accept:application/json' -X POST localhost:3000/simple_api -d '#{message.to_json}'' 

Hope this goes without saying, but don't use it in production. Try the Faraday stone, Mislav gives a weighty argument: http://mislav.uniqpath.com/2011/07/faraday-advanced-http/

+5
Nov 19 '13 at 14:11
source share

real life example, tell Airbrake API about new deployment via NetHttps

 require 'uri' require 'net/https' require 'json' class MakeHttpsRequest def call(url, hash_json) uri = URI.parse(url) req = Net::HTTP::Post.new(uri.to_s) req.body = hash_json.to_json req['Content-Type'] = 'application/json' # ... set more request headers response = https(uri).request(req) response.body end private def https(uri) Net::HTTP.new(uri.host, uri.port).tap do |http| http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end end project_id = 'yyyyyy' project_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' url = "https://airbrake.io/api/v4/projects/#{project_id}/deploys?key=#{project_key}" body_hash = { "environment":"production", "username":"tomas", "repository":"https://github.com/equivalent/scrapbook2", "revision":"live-20160905_0001", "version":"v2.0" } puts MakeHttpsRequest.new.call(url, body_hash) 

Notes:

in case you perform authentication through the Authorization header set header req['Authorization'] = "Token xxxxxxxxxxxx" or http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html

+5
Sep 05 '16 at 10:21
source share

I like this lightweight http request client called unirest

gem install unirest

using:

 response = Unirest.post "http://httpbin.org/post", headers:{ "Accept" => "application/json" }, parameters:{ :age => 23, :foo => "bar" } response.code # Status code response.headers # Response headers response.body # Parsed body response.raw_body # Unparsed body 
+2
Apr 05 '16 at 15:26
source share

Net / http api can be difficult to use.

 require "net/http" uri = URI.parse(uri) Net::HTTP.new(uri.host, uri.port).start do |client| request = Net::HTTP::Post.new(uri.path) request.body = "{}" request["Content-Type"] = "application/json" client.request(request) end 
0
May 22, '15 at 3:13
source share
 data = {a: {b: [1, 2]}}.to_json uri = URI 'https://myapp.com/api/v1/resource' https = Net::HTTP.new uri.host, uri.port https.use_ssl = true https.post2 uri.path, data, 'Content-Type' => 'application/json' 
0
Jul 16 '16 at 21:35
source share

You can send a json data array as follows:

 require 'rest_client' postData = Net::HTTP.post_form(URI.parse('http://www.xyz.com/api/valueoff'), {'data'=>jsonData}) 

For this, you must have a rest_client gem.

-2
Apr 27 '13 at 1:32
source share



All Articles