[英]ruby httparty get response url or id after post
如何在發布帖子后在單獨的腳本中使用httparty從rails項目獲取響應URL或ID?
紅寶石腳本:
HTTParty.post('http://localhost:3000/change_logs', parameters)
response.body和所有其他人不顯示網址和響應ID
兩年后,我找到了一種方法來訪問response
request
屬性的最后一個URI:
url = "http://example.com/redirects/to/www"
response = HTTParty.get(url)
response.request.last_uri.to_s
# => "http://www.example.com"
不幸的是,HTTParty沒有保留這些信息。 當遇到重定向時,它將遵循重定向並返回該請求的結果。 它不會保存原始請求中的任何信息。
好消息是,這樣的行為(通常)是可預測的。 在對URL發布請求后,Web應用程序通常會重定向到相同的內容。
但是如果你真的想這樣做,你將不得不使用Ruby附帶的net / http庫。 然而,它並不比HTTParty難得多,所以它不是更多的工作。
這對我有用,可能有更好的方法來做到這一點..
get_change_log_id = HTTParty.post('http://localhost:3000/change_logs.xml', parameters).to_a
get_change_log_id.each do |r|
r.each do |sub|
sub2 = sub.to_a
sub2.each do |s|
if s.index "id"
@change_log_id = s.to_s.sub(/[i]/, '').sub(/[d]/, '')
end
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.