Rails: get values of http response
Parse the response content with JSON.parse
:
require 'json'
...
data = JSON.parse(response.body)
data['button']['code'] # => "dfhdsg7f23hjgfs7be7"
Get HTTP POST Response in Rails Controler
The variable resp
represents the response object. You can use the #body
method to get the body of the response as String.
If the body is a String serialization of a JSON, simply parse it back to fetch the elements.
hash = JSON.parse(resp.body)
hash['token']
# => ...
rails best way to extract values from response hash
are you looking for something like this?
urls = _response["responseData"]["results"].map { |result| result["url"] }
How can I get the value in a hash of the response of POST request on Ruby?
You can parse it and fetch the result after it using the dig
method.
def send_param(param)
uri = URI.parse('https:~~~~~~~~~~~~')
https = Net::HTTP.new(uri.host, 443)
response = https.post(uri.path, param.to_query)
JSON.parse(response.body)
end
result = send_param({})
result.dig('result', 'name')
Check values in JSON response in Rails Controller
You should try
if params['protocol']["device1"] == true
Note : true
is without the quotes
Or if the value is always going to be boolean, you can simply do the following:
if params['protocol']["device1"]
How to access values from a get request in a hash (ruby)
The returned value of get is only a single element hash like {'red' => 3}
If you want to get the key and the value without accessing the entry using
get(var)['red']
=> 3
you could do the following:
get(var).keys.first
=> 'red'
get(var).values.first
=> 3
or you could take the return value and map it into a new hash:
new_hash = {"color" => get(var).keys.first, "id" =>get(var).values.first}
then access like this:
new_hash["color"]
=> red
new_hash["id"]
=> 3
Read the content of a div from a NET:HTTP response
I did this:
dialect = current_user.dialect.name
text = params[:text]
uri = URI('http://www.degraeve.com/cgi-bin/babel.cgi')
params = { :w => text, :d => dialect }
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
render inline: response.body.html_safe, layout: false if response.is_a?(Net::HTTPSuccess)
and then got the div that I wanted in a .js file.
Related Topics
Clicking Link with JavaScript in Mechanize
Trying to Compare Two Text Files, and Create a Third Based on Information
Soap Client for Ruby 1.9 and Rails
Upgraded to Ruby 1.9.2 and Getting Segmentation Fault Errors in Nokogiri
How to Remove Xcode 4.2 and Install 4.1 to Develop Ruby/Rails on Osx Lion
Zentest Autotest Not Running Tests
Rbenv Installed Ruby 2.1.2 Cannot Load Such File -- Zlib on MAC Osx 10.9.2
How to Delete Some Folders of Nokogiri and Capybara-Webkit Inside of My Rvm Gemset
What Are the Differences Between Lazy, Greedy and Possessive Quantifiers
Rails: Get Values of Http Response
How Does Ruby Allow a Method and a Class with the Same Name
Drop-Down-Menu for Many-To-Many Relation in Rails Using Nested Attributes
Xlsx Compressed by Rubyzip Not Readable by Excel
How to Call a Parent App's Helper Method from a Rails 3.1 Engine