node soap模塊
一、瀏覽器查看需要調(diào)用的接口
1、使用瀏覽器打開要調(diào)用的接口地址,后綴需要加上wsdl
2、找到需要調(diào)用的接口方法及入?yún)⒏袷饺缦?

image.png
二、可以使用soapUI工具進(jìn)行調(diào)用測(cè)試服務(wù)是否正常(入?yún)⒏袷接煞?wù)商提供)
1、soapUi加載wsdl地址
2、找到需要調(diào)用的方法
3、<![CDATA[]]>若節(jié)點(diǎn)數(shù)據(jù)中存在特殊字符可以使用

image.png

image.png
三、組織xml數(shù)據(jù)

image.png
四、使用soap模塊進(jìn)行調(diào)用
1、若不存在soap模塊需要使用進(jìn)行安裝
cnpm install soap
2、引用模塊
let soap = require('soap')
3、組織xml數(shù)據(jù),注意此處的xml = soapUi中的<tem:xml>
let data = {
xml: <Request>
<Common>
<DistrictCode></DistrictCode>
<ClientIp></ClientIp>
</Common>
<Recipes>
<Recipe>
<RecipeId></RecipeId>
</Recipe>
</Recipes>
</Request>
}
4、soap建立連接
// forceSoap12Headers 為設(shè)置合適的請(qǐng)求頭
soap.createClient(wsdl地址, {forceSoap12Headers: false}, function (err, client) {
//調(diào)用要調(diào)用的接口方法名,傳入組織好的xml數(shù)據(jù)
client['submitRecipe'](data, function (err, result) {
//獲取到返回參數(shù)后使用xml2js進(jìn)行數(shù)據(jù)格式化
//explicitArray 解決xml2js會(huì)默認(rèn)會(huì)把子子節(jié)點(diǎn)的值變?yōu)橐粋€(gè)數(shù)組
xml2js.parseString(result, {explicitArray: false},(err,result) =>{
//此處的result已經(jīng)是json格式
logger.info(result);
})
})
})
5、整體代碼
let soap = require('soap')
let xml2js = require('xml2js')
let data = {
xml:
<Request>
<Common>
<DistrictCode></DistrictCode>
<ClientIp></ClientIp>
</Common>
<Recipes>
<Recipe>
<RecipeId></RecipeId>
</Recipe>
</Recipes>
</Request>
}
return new Promise(function (resolve ,reject){
soap.createClient('wsdl', {forceSoap12Headers: false}, function (err, client) {
if(err){
reject(err);
}else{
client['submitRecipe'](data, function (err, result) {
if(err){
reject(err)
}else{
logger.info('--------xxxxxxxxx---------',result);
xml2js.parseString(result.submitRecipeResult, {explicitArray: false},(err,result) =>{
if(err){
logger.error(err);
}else{
logger.info('***********XML*************',result);
}
});
resolve(result);
}
});
}
});
});