tags:

views:

26

answers:

1

Hi,

I'm trying to get a random page from Wikipedia, using WikiMedia's documented Random method.

Safari has no problem getting the page: http://en.wikipedia.org/w/api.php?action=query&list=random&rnlimit=1&rnnamespace=0&format=json

But when I do, using Ruby HTTP/Net, I keep getting this exact error page: http://en.wikipedia.org/w/api.php (with the same "help" error code, and empty info).

url = URI.parse('http://en.wikipedia.org/w/api.php?action=query&list=random&rnlimit=1&rnnamespace=0&format=json')
req = Net::HTTP::Get.new(url.path, "User-Agent" => "ourbandiscalled")
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}

What is wrong with my code?

Thank you,

Kevin

+3  A: 

url.path only returns "/w/api.php" so you are losing the query string from your URL in the GET request. You can use url.request_uri instead e.g.

req = Net::HTTP::Get.new(url.request_uri, "User-Agent" => "ourbandiscalled")

The query string on its own is available as url.query. In summary:

irb(main):045:0> url.path
=> "/w/api.php"
irb(main):046:0> url.query
=> "action=query&list=random&rnlimit=1&rnnamespace=0&format=json"
irb(main):047:0> url.request_uri
=> "/w/api.php?action=query&list=random&rnlimit=1&rnnamespace=0&format=json"
mikej
Perfect! I don't know why I couldn't find it in the documentation. Thanks a lot, my mini-site is now available: http://ourbandiscalled.com :-)
Kevin