首页 > 解决方案 > 在将 XML 发送到 SOAP API 之前,如何在我的 XML 中放置一些变量?

问题描述

我有以下代码,我终于能够使用 XML 并调用 SOAP api

import { EventPattern } from '@nestjs/microservices';
import { InjectModel } from '@nestjs/mongoose';
import { json } from 'express';
import { Model } from 'mongoose';
import { stringify } from 'querystring';
import * as soap from 'soap';
import { product } from './product/product.model';
import * as fs from 'fs';
import * as path from 'path';
import * as xml2js from 'xml2js';

@Controller()
export class AppController {


 constructor(@InjectModel('product') private readonly productModel: Model<any>,) { }

 @EventPattern("next")
 async handleMessagePrinted(data: Record<any, any>) {
   console.log(data);


   console.log(data);

   this.sendSoap(data);

 }

 insertproduct(data: any) {
   stringify(data);
   return this.productModel.insertMany(data);

 }

 sendSoap(product: any) {
   let soapRequest = require('easy-soap-request');

   // example data
   let url = 'http://apisoap.com/product/';
   let sampleHeaders = {
     'user-agent': 'sampleTest',
     'Content-Type': 'text/xml;charset=UTF-8',
     'soapAction': '',
   };
   let xml = 'Createproduct.xml';
   
   fs.readFile(xml, "utf-8", function(err, data) {
     if (err) console.log(err);
     // we log out the readFile results
     // we then pass the data to our method here
     xml2js.parseString(data, function(err, result) {
       if (err) console.log(err);
       // here we log the results of our xml string conversion
       console.log("result: "+result);
     });
   });
   
   // usage of module
   
   let { response } = soapRequest({ url: url, headers: sampleHeaders, xml: xml, timeout: 1000 }); // Optional timeout parameter(milliseconds)
   return response;
  

 }

 


}

但现在我没有找到一种在将调用发送到 API 之前将一些变量放入该 XML 的方法。

我的最终目标是使用我收到的 JSON 中的一些变量并将它们放在 XML 的正确位置

标签: node.jsxmlsoapnestjs

解决方案


推荐阅读