我使用ruby Net::HTTP.post_form
将XML发布到ConnectWise服务器,使用CompanyAPI。我得到了一个错误:
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
从我能找到的所有东西来看,我可能在<?xml version="1.0" encoding="utf-8"?>
前有一个空格或换行符。
以下是代码:
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
以下是带有错误的完整输出:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><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. ---> 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& abortProcessing)
--- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
如果我只是打印到一个文件,它似乎是好的,我如何找到空白或换行符(或任何其他可能在第1行)并删除它?
下面是在ConnectWise API文档中提供的一个示例XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<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>
发布于 2015-04-16 14:51:39
它与空格无关。您的XML是完全不正确的。问题在于字符串:
xml.tag!('GetCompany xmlns="https://'+@cwhostname+'"')
显然,它将整个字符串视为一个标记,生成结束标记为:
</GetCompany xmlns="https://connectwise.com">
在代码中的任何地方,puts cw_company_api_get_company
都可以很容易地看到这一点。属性将传递给构建器标记,如下所示:
xml.tag!('GetCompany', :xmlns => "https://#{@cwhostname}")
另一个问题是您需要将id
放在GetCompany
标记中。总结:
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
希望这能有所帮助。
https://stackoverflow.com/questions/29675453
复制相似问题