Ruby Net::Http::Get and JSON Responses

Ruby Net::HTTP::Get and JSON responses

You need to actually send the request and retrieve a response object, like this:

response = Net::HTTP.get_response("example.com","/?search=thing&format=json")
puts response.body //this must show the JSON contents

Regards!

PS: While using ruby's HTTP lib, this page has some useful examples.

Convert HTTP response to JSON in Ruby

Step 1: Parse the GET response:

require 'cgi'

CGI::parse(MYSTRING)

Returns: {"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}

Step 2. Convert to JSON:

require 'json'

myObject.to_json

Alternatively, look at this snippet:

https://gist.github.com/timsavery/1657351

require "rubygems"
require "json"
require "net/http"
require "uri"

uri = URI.parse("http://api.sejmometr.pl/posiedzenia/BZfWZ/projekty")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)

if response.code == "200"
result = JSON.parse(response.body)

result.each do |doc|
puts doc["id"] #reference properties like this
puts doc # this is the result in object form
puts ""
puts ""
end
else
puts "ERROR!!!"
end

Posting Ruby data in JSON format with Net/http

Make a request object like so:

request = Net::HTTP::Post.new(uri.request_uri, 
'Content-Type' => 'application/json')
request.body = newAcctJson
resp = http.request(request)

How do I capture JSON response from an API query into a separate output.json file when the below script runs ? #Beginner #Ruby

require 'uri'
require 'pathname'
require 'net/http'

# This url does not seem to work - tested with example.com
url = URI("https://jakunamatata.net/v1/jakunamatata-analytics/summary")
path = Pathname.new(Dir.pwd).join('output.json')

Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(url)
http.request(request) do |response|
File.open(path, 'w') do |io|
response.read_body do |chunk|
io.write(chunk)
end
end
end
end

puts "File downloaded to #{path}."

This streams the download directly to disk instead of reading the response body into memory. Once you get tired of reinventing the wheel checkout the down gem.

JSON string pulled via net/http to Hash

As you can see, the field you are looking for is into an inner hash. Try

puts json_hash["query"]["results"]["rate"]["Rate"]

How can I get the response of request of API on ruby?

You can parse the response body:

JSON.parse(res.body)

The example of getting results from JSONPlacehoder service in a pure IRB session.

irb(main):001:0> require 'json'
=> true
irb(main):002:0> require 'net/http'
=> true
irb(main):003:0> require 'uri'
=> false
irb(main):004:1* def api(url)
irb(main):005:1* uri = URI.parse(url)
irb(main):006:1* req = Net::HTTP::Get.new(uri.request_uri)
irb(main):007:1* https = Net::HTTP.new(uri.host, uri.port)
irb(main):008:1* https.use_ssl = true
irb(main):009:1* res = https.request(req)
irb(main):010:1* JSON.parse(res.body)
irb(main):011:0> end
=> :api
irb(main):012:0> url = 'https://jsonplaceholder.typicode.com/todos'
irb(main):013:0> api(url)
=> [{"userId"=>1, "id"=>1, "title"=>"delectus aut autem", "completed"=>false}, {"userId"=>1, "id"=>2, "title"=>"quis ut nam facilis et officia qui", "completed"=>false}, {"userId"=>1, "id"=>3, "title"=>"fugiat veniam minus", "completed"=>false}]


Related Topics



Leave a reply



Submit