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
url.pathonly returns"/w/api.php"so you are losing the query string from your URL in the GET request. You can useurl.request_uriinstead e.g.The query string on its own is available as
url.query. In summary: