0

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
  • Where is that error originating (file and line number)? I don't see anything in your code that would cause that error. It's the result of doing something like `1 + "hello"`. Also, you're instantiating @xml and @catalogs but then you don't do anything with @catalogs. So the @xml your posting is essentially an empty document. – Tom L Apr 12 '18 at 18:23
  • Hey Tom, ive updated the question to also contain the error extract and also trace with the line error as well as the xml.builder file im using which contains all the XML i want to send. My idea was to create all the XML in the builder file, then have this data stored within the variable @XML and then push this to the url in question – Tom Haythorn Apr 13 '18 at 03:32
  • If the error is happening in the post method then it has to be a problem in the arguments your passing. Either the url or @xml, I'd imagine. – Tom L Apr 13 '18 at 18:40

0 Answers0