I'm trying to download account transactions (an XML file) from a server. When I enter this URL from a browser:
https://secure.somesite.com:443/my/account/download_transactions.php?type=xml
it successfully downloads a correct XML file (assuming I've already logged in).
I want to do this programmatically with Ruby, and tried this code:
require 'open-uri'
require 'rexml/document'
require 'net/http'
require 'net/https'
include REXML
url = URI.parse("https://secure.somesite.com:443/my/account/download_transactions.php?type=xml")
req = Net::HTTP::Get.new(url.path)
req.basic_auth 'userid', 'password'
req.content_type = 'text/xml'
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
response = http.start { |http| http.request(req) }
root = Document.new(response.read_body).root
root.elements.each("transaction") do |t|
id = t.elements["id"].text
description = t.elements["description"].text
puts "TRANSACTION ID='#{id}' DESCRIPTION='#{description}'"
end
Execution proceeds, but fails on the "Document.new":
RuntimeError: Illegal character '&' in raw string "??ࡱ?;??
The returned body is clearly not XML if printed, and appears to be a long string of mostly unreadables, with the occasional visible word indicating it has something to do with the intended content. I also see the string "Arial1" mixed in with the unreadables several times, which makes me think I'm receiving a format other than XML.
My question is, what am I doing wrong here? The XML file is definitely available (and correct if you examine the browser-obtained copy). Am I specifying something wrong with the SSL? The HTTPS request? Is there a different and proper way to reveal the correct body? Thanks in advance for your assistance!