简体   繁体   English

Rails:解析HTTParty

[英]Rails: Parsing HTTParty

I've read other answers on this topic, such as: 我已经阅读了有关此主题的其他答案,例如:

Parsing HTTParty response 解析HTTParty响应

HTTParty parsing JSON in Rails HTTParty在Rails中解析JSON

However, I still can't figure out how to parse a response I'm receiving. 但是,我仍然不知道如何解析我收到的响应。

response.parsed_response = HTTParty.get(url, query: params) returns: response.parsed_response = HTTParty.get(url, query: params)返回:

=> #<HTTParty::Response:0x89435d0 parsed_response="http://foo.com", @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"cache-control"=>["no-cache", "no-store"], "date"=>["Tue, 21 Feb 2017 23:10:47 GMT"], "expires"=>["Thu, 01 Jan 1970 00:00:00 GMT"], "p3p"=>["CP=\\"ALL IND DSP COR CUR ADM TAIo PSDo OUR COM INT NAV PUR STA UNI\\""], "pragma"=>["no-cache"], "server"=>["Apache-Coyote/1.1"], "set-cookie"=>["bar.Agent.p=c1921b97d1f8a0918621c48bd32ded2b; Domain=.bar.com; Expires=Fri, 19-Feb-2027 23:10:47 GMT; Path=/"], "content-length"=>["366"], "connection"=>["Close"]}>

I need the URL that appears after parsed_response . 我需要parsed_response之后出现的URL。 The other answers seemed to break down hashes that appear after parsed_response, but I'm just looking for the url that appears after parsed_response (and it only appears there in the response). 其他答案似乎可以解决parsed_response之后出现的哈希,但我只是在寻找parsed_response之后出现的URL(并且它仅出现在响应中)。

I tried: puts response which returns the entire response above. 我试过了: puts response返回上面的整个响应。

puts response.parsed_response which returns: puts response.parsed_response返回:

http://foo.com
=> nil

This usually works for me 这通常对我有用

response = HTTParty.get(url, options)
puts response.body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM