Rails:获取http响应的值 [英] Rails: get values of http response

查看:120
本文介绍了Rails:获取http响应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rails中我发了一个api的帖子并得到了回复,但我不知道如何获取信息并找到code变量的值。

In rails I made a post to an api and got a response, but I don't know how to get through the information and find the value of the "code" variable.

$ response 
#<Net::HTTPOK 200 OK readbody=true>

$ puts response.body
{"success":true,"button":{"code":"dfhdsg7f23hjgfs7be7","type":"buy_now","style":"none","text":"Send to MeBrah","name":"MeBrah","description":"Coins you&#x27;re willing to give me.","custom":"6","callback_url":null,"success_url":null,"cancel_url":null,"info_url":null,"auto_redirect":false,"price":{"cents":40000000,"currency_iso":"BTC"},"variable_price":true,"choose_price":false,"include_address":false,"include_email":false}}

$ response.code
"200"  

我知道代码变量也相等dfhdsg7f23hjgfs7be7我只是好奇什么命令会返回它的值。

I know the code variable is equal too "dfhdsg7f23hjgfs7be7" I'm just curious what command would return it's value.

推荐答案

使用 JSON.parse

require 'json'

...

data = JSON.parse(response.body)
data['button']['code'] # => "dfhdsg7f23hjgfs7be7"

这篇关于Rails:获取http响应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆