我有两个rails应用程序。应用1将从控制器操作发送的POST请求发送到应用程序2中的另一个控制器操作。我希望能够在应用1中读取对该帖子的响应。
App #1控制器:
require 'net/http'
# get the url that we need to post to
url = URI.parse('http://app2.com/sessions/login_request')
# build the params string
post_args1 = {
'username' => 'my@test.com'
}
# send the request
resp, data = Net::HTTP.post_form(url, post_args1)
#HOW do I read :token and :tokenMnemonic here??
App 2控制器:
def login_request
# do some logic here
render :json => {
:result => "OK",
:token => random_token,
:tokenMnemonic => tokenMnemonic
}
end
问题是如何从App #1控制器接收到的POST响应中读取:token和:tokenMnemonic。
发布于 2014-11-03 06:26:55
变量resp
表示响应对象。您可以使用#body
方法以字符串的形式获取响应体。
如果主体是JSON的字符串序列化,只需解析它以获取元素。
hash = JSON.parse(resp.body)
hash['token']
# => ...
https://stackoverflow.com/questions/26716079
复制相似问题