[英]Access Response from HTTParty Request
I'm obtaining a token from an external API via HTTParty. 我正在通过HTTParty从外部API获取令牌。
My call looks like this: 我的电话看起来像这样:
token_request = HTTParty.post(@url, body: token_payload.to_json, headers: { 'Content-Type' => 'application/json' })
I receive back the following JSON: 我收到以下JSON:
{"token":"TBpdV20Fsdbycgmib2B8ZhasVnRb","expiration_utctimestamp":"23123202226","error_code":0,"error_message":null}
I now want to pass the token value into my next request. 我现在想将令牌值传递到下一个请求中。
How do I get the "TBpdV20Fsdbycgmib2B8ZhasVnRb" bit into a variable? 如何将“ TBpdV20Fsdbycgmib2B8ZhasVnRb”位放入变量中? My guess is this, but doesn't work:
我的猜测是这样,但是不起作用:
token = token_request[:token]
Thanks 谢谢
HTTParty contains helper method that returns parsed JSON as a hash (if server responds with appropriate header). HTTParty包含帮助程序方法,该方法将解析后的JSON作为哈希返回(如果服务器以适当的标头响应)。 So you can access token via:
因此,您可以通过以下方式访问令牌:
token = token_request.parsed_response['token']
Another way in this particular case (server does not return application/json
header): 在这种特殊情况下的另一种方式(服务器不返回
application/json
标头):
token = JSON.parse(token_request.to_s)['token']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.