0
require 'net/http'
require 'uri'

@url = 'http://foobar.com'
@query_string = {foo1: 'bar1', foo2: 'bar2'}

@post_data = Net::HTTP.post_form(URI.parse(@url), @query_string)
@request = # ? How can I get the request URL + query?
@response = @post_data.body

Does anyone know how you can get the actual URL that you queried, not just the response?

i.e. I want to store this in a variable to record what was sent:

http://foobar.com?foo1=bar1&foo2=bar2

MichaelHajuddah
  • 547
  • 5
  • 18

3 Answers3

0

It's not getting it from the response itself (I don't think there is a way to do this with net/http), but you can get the variable you want by doing this:

@request = URI.parse(@url).tap{|x|x.query=URI.encode_www_form(@query_string)}.to_s
# => "http://foobar.com?foo1=bar1&foo2=bar2"
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
0

I believe you want to look into Ruby's URI Module:

http://www.ruby-doc.org/stdlib-2.0/libdoc/uri/rdoc/URI.html

Not sure, but you could probably get away with something like:

query_string = URI.encode_www_form(@query_string).to_s
record_of_uri = "#{@url}/?#{query_string}"
RubeOnRails
  • 1,153
  • 11
  • 22
0

Noting that a post request doesn't send it's params in the url, to compose @request as described use:

@request = URI.escape(@url + '?' + URI.encode_www_form(@query_string))
rudolph9
  • 8,021
  • 9
  • 50
  • 80