SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    whats the big deal missing a slash?

    In the following code,

    Code:
    require 'net/http'
    url = URI.parse('http:...ww.google.com/')
    response = Net::HTTP.start(url.host, url.port) do |http|
    http.get(url.path)
    end
    content = response.body       
    puts content
    if I miss the / it gives an error.
    Could someone explain why?

    Any advantage of using the above code than the following shell command
    Code:
    content=$(curl http:...w.google.com)
    and curl does not complain about the missing /

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Neither url is valid, so at this point the trailing slash is irrelevant. If instead you specify a url like this:

    Code:
    require 'net/http'
    
    url = URI.parse('http://www.google.com')
    puts url.host
    puts "-->#{url.path}<--"
    
    response = Net::HTTP.start(url.host, url.port) do |http|
      http.get(url.path)
    end
    
    content = response.body       
    puts content[0..100]
    
    --output:--
    www.google.com
    --><--
    `initialize': HTTP request path is empty (ArgumentError)
    the error message explains that url.path is a blank string, and get() requires something other than a blank string for the path.

    A url like:

    specifies the host as

    and the path as:

    /
    If a url like "http://www.google.com" works with curl, then that means curl uses '/' as the default path when no path is provided.

    any advantage of using the above code than the following shell command
    [code]content=$(curl http:...w.google.com)[/code
    That line isn't valid ruby, so it couldn't be used in a ruby program?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •