1

I would like to pass the variable that I get from a post request into xml body to make a call to a webservice. How do I pass javascript variables to xml ?

router.post('/', async (req, res) => {
const sorguNo= req.body.sorguNo; 

the variable that I get from post request

      const url = 'Url';
       const headers = {
      'Content-Type': 'text/xml; charset=utf-8',
      'soapAction': 'Soap Action'
         };
    const 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:Header>' +
        '<AuthHeader xmlns="http:">' +
          '<userName>username</userName>' +
           '<password>password</password>' +
        '</AuthHeader>' +
      '</soap:Header>' +
    '<soap:Body>' +
        '<xmlns="http:..">' +
          '<sonucNo></sonucNo>' + 

here I would like to use the variable in sonucNo field

        '</>' +
      '</soap:Body>' +
    '</soap:Envelope>';
2
  • 1
    Your xlm is just a string. You can use template strings to create a multi-line string where you can insert variables. Commented Feb 19, 2019 at 15:44
  • As you said using backticks solved the problem. Thanks ! Commented Feb 19, 2019 at 15:48

1 Answer 1

2

This can be done using template literals:

const sonucNo = "hello";
const xml = `<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope>
    <sonucNo>${sonucNo}</sonucNo>
  </soap:Envelope>`;

console.log(xml);

Sign up to request clarification or add additional context in comments.

1 Comment

This is vulnerable to xml injection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.