How can I get the response url or id from a rails project with httparty in a separate script after doing a post?
ruby script:
HTTParty.post('http://localhost:3000/change_logs', parame开发者_高级运维ters)
response.body and all the others ones do not show the url and the response id
Two years later, I found a way to access the last URI from the request
attribute on the response
:
url = "http://example.com/redirects/to/www"
response = HTTParty.get(url)
response.request.last_uri.to_s
# => "http://www.example.com"
Unfortunately, that information is not kept by HTTParty. When it encounters a redirect, it will follow the redirect and return the result of that request. It will not save any of the information from the original request.
The good news is, behavior like this is (usually) predictable. Web apps generally redirect to the same things after a post request to a URL.
But if you really want to do this, you're going to have to use the net/http library that comes with Ruby. It's not much harder than HTTParty though, so it's not much more work.
This works for me, there's probably a better way to do this though..
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
精彩评论