tags:

views:

122

answers:

2

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', parameters)

response.body and all the others ones do not show the url and the response id

+1  A: 

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.

AboutRuby
thanks for the response, that makes sense - however I did figure out a way to do it.. it's a little weird though
Nick
A: 

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
Nick
yes there was an easier way:results = HTTParty.post('http://localhost:3000/change_logs.xml', parameters)change_log_id = results.parsed_response["change_log"].values_at("id")
Nick