簡體   English   中英

Rails:獲取http響應的值

[英]Rails: get values of http response

在Rails中,我在api上發布了一個帖子並得到了響應,但是我不知道如何獲取信息並找到“ code”變量的值。

$ 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”,我很好奇什么命令會返回它的值。

使用JSON.parse解析響應內容:

require 'json'

...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM