如何找到隐藏的白色 space,或 "Net::HTTP.post_form" 内的换行 XML?
How can find the hidden white space, or new line within the "Net::HTTP.post_form" for XML?
我正在使用 ruby gem Net::HTTP.post_form
到 post XML 到 ConnectWise 服务器,使用公司 API。我收到此错误:
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
根据我能找到的所有内容,我可能在 <?xml version="1.0" encoding="utf-8"?>
之前有一个白色 space 或换行符
代码如下:
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>
如果我只是打印到一个文件,它看起来没问题,我如何找到白色的 space 或换行符(或任何其他可能位于第 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>
与空格无关。您的 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
希望对您有所帮助。
我正在使用 ruby gem Net::HTTP.post_form
到 post XML 到 ConnectWise 服务器,使用公司 API。我收到此错误:
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>
如果我只是打印到一个文件,它看起来没问题,我如何找到白色的 space 或换行符(或任何其他可能位于第 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>
与空格无关。您的 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
希望对您有所帮助。