ruby httparty在post后获取响应url或id

ruby httparty在post后获取响应url或id,ruby,httparty,Ruby,Httparty,在完成post后,如何在单独的脚本中从带有httparty的rails项目中获取响应url或id ruby脚本: HTTParty.post('http://localhost:3000/change_logs', parameters) response.body和所有其他文件都不显示url和响应id,不幸的是,该信息不由HTTParty保存。当它遇到重定向时,它将跟随重定向并返回该请求的结果。它不会保存原始请求中的任何信息 好消息是,这样的行为(通常)是可以预测的。Web应用程序通常在

在完成post后,如何在单独的脚本中从带有httparty的rails项目中获取响应url或id

ruby脚本:

  HTTParty.post('http://localhost:3000/change_logs', parameters)

response.body和所有其他文件都不显示url和响应id,不幸的是,该信息不由HTTParty保存。当它遇到重定向时,它将跟随重定向并返回该请求的结果。它不会保存原始请求中的任何信息

好消息是,这样的行为(通常)是可以预测的。Web应用程序通常在向URL发送post请求后重定向到相同的内容


但是如果你真的想这样做,你必须使用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

两年后,我找到了从
响应
上的
请求
属性访问最后一个URI的方法:

url      = "http://example.com/redirects/to/www"
response = HTTParty.get(url)
response.request.last_uri.to_s
# => "http://www.example.com"

谢谢你的回复,这很有道理——不过我确实想出了一个办法。。这有点奇怪,是的,有一种更简单的方法:results=HTTParty.post(“”,parameters)change_log_id=results.parsed_response[“change_log”]。如果您想在不获取最终目的地正文的情况下执行此操作,请将值设置为(“id”)。i、 e.如果您只想获取最终URL,可以将
HTTPParty.get
调用替换为
HTTPParty.head(URL,{:maintenance\u method\u overs\u redirects=>true})