I'm on an mission create an XML file and then POST this file to a server address. However I cant crack why it wont send, im currently stuck getting a "TypeError in CatalogController#gen_xml" > "String can't be coerced into Integer".
However im quite new to this and dont fully grasp to how to best execute this. Any help would be greatly appreciated!
Controller
class CatalogController < ApplicationController
require "builder/xmlmarkup"
require 'builder'
require 'rubygems'
require 'net/http'
def gen_xml
@xml = Builder::XmlMarkup.new
@catalogs=Catalog.all
url = "https://xxx.xxxx.com/xxxCXMLxx.aw/xx/cxml";
request = Net::HTTP::Post.new(url)
#request.add_field "Content-Type", "application/xml"
request.body = @xml
url = URI.parse(url)
req = Net::HTTP::Post.new(url.request_uri)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
#response = http.request(req)
response = http.post(url, @xml, initheader = {'Content-Type' =>'text/xml'})
end
end
View with Button to call method
Super cXML Generator!
<%= form_tag catalog_gen_xml_path, method: :post do %>
<%= submit_tag 'Call Action' %>
<% end %>
Routes File
Rails.application.routes.draw do
get 'catalog/gen_xml'
post 'catalog/gen_xml'
end
Error Trace
Extracted source (around line #24):
22 http.use_ssl = (url.scheme == "https")
23 #response = http.request(req)
24 response = http.post(url, @xml, initheader = {'Content-Type' =>'text/xml'})
25 end
26 end
27
app/controllers/catalog_controller.rb:24:in `gen_xml'
Rails.root: /Users/i303072/railsapps/xmlbuilder
XML Builder File - gen_xml.builder
@xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
#xml_markup.declare! :DOCTYPE, :chapter, :SYSTEM, "../dtds/chapter.dtd"
xml.cXML(:payloadID=>"XX", :timestamp=>"2018-03-11T11:28:01-07:00", :version=>"1.2.029", :xmllang=>"en-US") {
xml.Header {
xml.From {
xml.Credential(:domain=>"ID") {
xml.Identity "AN-T"
}#Credential
}#From
xml.To {
xml.Credential(:domain=>"ID") {
xml.Identity "AV-T"
}#Credential
xml.Correspondent{
xml.Contact(:role=>"correspondent") {
xml.Name("MRO", "xml:lang" => "en")
xml.PostalAddress{
xml.Country("US", "isoCountryCode" => "US")
}#PostalAddress
}#Contact
}#Correspondent
}#To
xml.Sender {
xml.Credential(:domain=>"NetworkID") {
xml.Identity "AN-T"
xml.SharedSecret "xxx"
}#Credential
xml.UserAgent "xxx"
}#Sender
} #Header
xml.Message(:deploymentMode=>"test") {
xml.ProductActivityMessage(:subcontractingIndicator=>"yes") {
xml.ProductActivityHeader(creationDate: "2018-03-13T22:00:00-08:00", messageID: "Cxxx")
}#ProductivityActivityMessage
}#Message
}#cXML