如何在发布帖子后在单独的脚本中使用httparty从rails项目获取响应URL或ID?
红宝石脚本:
HTTParty.post('http://localhost:3000/change_logs', parameters)
Run Code Online (Sandbox Code Playgroud)
response.body和所有其他人不显示网址和响应ID
Ava*_*iri 29
两年后,我找到了一种从以下request属性访问最后一个URI 的方法response:
url = "http://example.com/redirects/to/www"
response = HTTParty.get(url)
response.request.last_uri.to_s
# => "http://www.example.com"
Run Code Online (Sandbox Code Playgroud)
nic*_*rix -1
这对我有用,但可能有更好的方法来做到这一点..
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
Run Code Online (Sandbox Code Playgroud)