I am trying to use rails 4.0 to create an API for a web service which requires a direct xml post to their secure server.
I have a form which submits the `:action => "testSubmit" to trigger the following code in my controller, which I've pieced together after hours of (mostly outdated) research and debugging.
def testSubmit
xml = Builder::XmlMarkup.new
xml.instruct!
xml.sale do
xml.tag! 'tag-1', "INFO_HERE"
xml.tag! 'tag-2', "MORE_INFO"
xml.amount "100.00"
end
uri = URI('https://secure.abc.com')
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
response = http.post("/api/some/path", xml.to_s)
flash[:notice] = "Sent?"
end
end
The rails framework does redirect to the testSubmit.html.erb
view, with the flash
present, so everything indicates that it may be working, but if I try to read the response data it appears to be empty. For instance if I include <%= response %>
in my view, I see:
#<ActionDispatch::Response:0x007fd167ddef60>
but <%= response.body %>
renders nothing...
I expect the response to be specific xml, which I have verified using PHP and Curl, as well as the firefox add-on "Poster"
Solved it. Working code below.
def testSubmit
xml = Builder::XmlMarkup.new
xml.instruct!
xml.sale do
xml.tag! 'tag-1', "INFO_HERE"
xml.tag! 'tag-2', "MORE_INFO"
xml.amount "100.00"
end
uri = URI('https://secure.abc.com')
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
@xml = xml.target!
@response = http.post(uri.path, @xml, initheader = {'Content-Type' =>'text/xml'})
flash[:notice] = "Sent?"
end
end
This: <%= @response.body %>
Works fine...