Rails: Get Values of Http Response

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



Leave a reply



Submit