TypeError: can't convert Net::HTTPOK into String
You want
result = JSON.parse(response.body)
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html TypeError problem: no implicit conversion in Sinatra + JSON.parse
It's possible the library is treating the response like text. Try adding an Accept header. This worked for me:
request["Accept"] = "application/json"
example:
uri = URI.parse("https://api.github.com")
req = Net::HTTP::Get.new(URI("https://api.github.com/repos/JJ/microservices-broker/compare/d5d39c5db99d...bbbf695d1bf2"))
req["Accept"] = 'application/json'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(req)
json = JSON.parse(response.body)
json['url']
# or
json = JSON.parse(response.body, symbolize_names: true)
json[:url]
(EDIT:) Also, Using Net::HTTP is really painful. Please checkout these libraries:https://github.com/lostisland/faraday
https://github.com/octokit/octokit.rb
how deserialize a json in rails
Net::HTTP has responses in body
. so, Try
JSON.parse response.body
Ruby request to https - in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
There are several oddities in your code. The main is: since you use SSL
you are to aknowledge HTTP.start
about with :use_ssl => url.scheme == 'https'
. HTTP.Get
constructor awaits for an URI
, not the path. The summing up:
domain = 'http://www.google.com'
url = URI.parse("https://graph.facebook.com/fql?q=SELECT%20url,normalized_url%20FROM%20link_stat%20WHERE%20url='#{domain}'")
req = Net::HTTP::Get.new url
res = Net::HTTP.start(url.host, url.port,
:use_ssl => url.scheme == 'https') {|http| http.request req}
puts res
Gives:#<Net::HTTPOK:0x000000027d0558>
ruby: Extracting fields from nested json
Try this:
# just for demonstration, you would probably do json = JSON.parse(response)
json = {
"status": 200,
"data": {
"total": 251,
"alerts": [
{
"dataPoint": "x",
"ackedBy": "x",
...
For just the total:total = json['data']['total']
For the other values you asked about:json['data']['alerts'].each do |alerts|
# do whatever you want with these...
alerts['dataPoint']
alerts['startOn']
alerts['ackedOn']
Now the question is, what do you want to do with the results? Do you want to collect them into a new hash? The json['data']['alerts']
is an array so you have to decide how you want to collect them. You could do:collected_alerts = { 'dataPoints' => [], 'startOns' => [], 'ackedOns' => [] }
json['data']['alerts'].each do |alerts|
collected_alerts['dataPoints'] << alerts['dataPoint']
collected_alerts['startOns'] << alerts['startOn']
collected_alerts['ackedOns'] << alerts['ackedOn']
end
Now you can get all those values in collected_alerts
How do I resolve a cors error on a fetch or 401?
- Browser extensions are notoriously useless at dealing with preflighted requests. It won't help here.
- No-cors mode makes the browser silently fail anything that requires CORS permission instead of throwing an exception. It won't help here.
- Postman isn't a browser. It's requests aren't triggered by visiting a website. It doesn't implement the Same Origin Policy and doesn't need permission from CORS.
I'm getting a 401 Preflight which indicates unauthorized but I'm setting the Authorization header with the correct credentials so not sure what's happening here.The browser is making a preflight request asking permission to send a POST request with credentials.
The server is getting the preflight request and complaining that it doesn't have credentials on it.
Either:
- Change the server you are making the request to so it grants you permission in your development environment. It needs to allow OPTIONS requests without credentials on them.
- Use a proxy that relays your requests to the server
Related Topics
In Ruby, How to Be Warned of Duplicate Keys in Hashes When Loading a Yaml Document
Is The Assignment Operator Really "Just" an Operator
How Does String.Unpack Work in Ruby
How to Get Source and Variable Values in Ruby Tracebacks
Ruby Fails on Osx Lion with Rbenv
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
Many to Many Table with an Extra Column in Rails
Statically Compile Pdftk for Heroku. Need to Split PDF into Single Page Files
Without Converting to a String, How Many Digits Does a Fixnum Have
What Is 'stringify_Keys' in Rails and How to Solve It When This Error Comes
Using Rails with Paperclip and Swfupload
Algorithm to Render a Horizontal Binary-Ish Tree in Text/Ascii Form
Pg.Rb Segmentation Fault [Mojave Upgrade]
Spawning an Independent Thread or Process in Ruby
Belongs_To Presence in Rails 5 Not Working
Rails - Understanding Application.Js and Application.CSS
Convert Durations in Ruby - Hh:Mm:Ss.Sss to Milliseconds and Vice Versa
Ruby: How to Convert an Array of Data to Hash and to JSON Format