ruby httparty在帖子后获得回复网址或ID

nic*_*rix 16 ruby httparty

如何在发布帖子后在单独的脚本中使用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)

  • 如果你想在没有获得最终目的地的身体的情况下这样做.即如果你想获得最终的URL,你可以用`HTTParty.head(url,{:maintain_method_across_redirects => true})替换`HTTPParty.get`调用. (3认同)

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)