jeudi 16 avril 2015

How can find the hidden white space, or new line within the "Net::HTTP.post_form" for XML?

I'm using the ruby gem Net::HTTP.post_form to post XML to a ConnectWise CompanyAPI server. I'm getting this error:



System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.


From everything I can find I likely have a whitespace or newline character before the <?xml version="1.0" encoding="utf-8"?>


Here is the code:



require 'net/http'
require 'uri'
require 'builder'

#f = File.new('cw.get_company.xml', 'w')
@cwhostname = 'cw_fqdn'
companyapi_url = 'https://' + @cwhostname + '/v4_6_release/apis/2.0/CompanyApi.asmx'
uri = URI(companyapi_url)

def cw_company_api_get_company
companyid = 'company_name'
integratorloginid = 'inegrator_login_id'
integratorpassword = 'inegrator_passwd'
companyidint = 0

xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.tag!('soap:Envelope'){
xml.tag!('soap:Body'){
xml.tag!('GetCompany xmlns="https://'+@cwhostname+'"'){
xml.tag!('credentials'){
xml.CompanyId(companyid)
xml.IntegratorLoginId(integratorloginid)
xml.IntegratorPassword(integratorpassword)
}
}
xml.id(companyidint)
}
}
end

response = Net::HTTP.post_form(uri,[cw_company_api_get_company])
#puts cw_company_api_get_company
puts response.body


Here is the full output with error:



<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://ift.tt/18hkEkn" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
--- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>


If I just print to a file it appears to be fine, how can I find the white space or newline (or whatever else could be at line 1) and remove it?


Here is a sample XML provided in the ConnectWise API documentation:



<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap="http://ift.tt/sVJIaE">
<soap:Body>
<GetCompany xmlns="http://connectwise.com">
<credentials>
<CompanyId>string</CompanyId>
<IntegratorLoginId>string</IntegratorLoginId>
<IntegratorPassword>string</IntegratorPassword>
</credentials>
<id>int</id>
</GetCompany>
</soap:Body>
</soap:Envelope>

Aucun commentaire:

Enregistrer un commentaire