Getting URL from request object using net::http

I am creating a request object (my_request) and getting a response from it.

  if params[:method].upcase == 'GET'
    my_request = Net::HTTP::Get.new('/' + params[:url]) 
  elsif params[:method].upcase == 'POST'
    my_request = Net::HTTP::Post.new('/' + params[:url]) 
  elsif params[:method].upcase == 'PUT'
    my_request = Net::HTTP::Put.new('/' + params[:url]) 
  elsif params[:method].upcase == 'DELETE'
    my_request = Net::HTTP::Delete.new('/' + params[:url]) 
  end

  response = http.request(my_request)

I want to get the exact URL where my_request is going to make a request. I tried 'fullpath' and original_url, but none of them work.

Any ideas?

Answers


Net::HTTP classes don't have a fullpath or original_url method or an equivalent. They inherit from Net::HTTP::GenericRequest, which has no such method.

Instead, you should use the path method. Net::HTTP requests don't store a hostname, so if you want to make a valid request, you need to include the full hostname, not just the path.

Example
# Good: complete path
request = Net::HTTP::Get.new('http://example.com/about')
request.path # => 'http://example.com/about'
http.request(request) # => Success

# Bad: incomplete path
request = Net::HTTP::Get.new('/about')
request.path # => '/about'
http.request(request) # => Error

Also, if it were mine, I'd refactor your code to look more like this:

url = "http://hostname.com/" + params[:url]
request = case params[:method].upcase
          when 'GET'    then Net::HTTP::Get.new(url)
          when 'POST'   then Net::HTTP::Post.new(url)
          when 'PUT'    then Net::HTTP::Put.new(url)
          when 'DELETE' then Net::HTTP::Delete.new(url)
          end

http.request(request)

Need Your Help

Is there some kind of equivalent to .NET's BackgroundWorker in Vala?

linux multithreading gtk vala

I'm trying to learn Vala so I'm making a small GUI application. My main language before has been C# so things are going pretty well.

Deserializing XML to Object always null

c# xml xml-serialization

I can not seem to get my head around this. I am trying to deserialize some xml to an object and my test comes out negative, as in all properties return null.