Trinitycore: SOAP template example

Created on 26 Jan 2015  路  4Comments  路  Source: TrinityCore/TrinityCore

Can someone show me a example of a SOAP request to TrinityCore? How this works? I wanna know that to make a non-wsdl connection from Node.js, there are only PHP examples, and I don't know how should I create my XML body...

Most helpful comment

I found that the example above no longer works (not for me at least). So I made an updated version:

const config = {
    ip: '127.0.0.1',
    port: 7878,
    username: 'username',
    password: 'password'
}

// Everything needed to send a message via SOAP to the worldserver
const fetch = require('node-fetch');

const formatSoap = (command) => `
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:TC" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:executeCommand>
            <command>${command}</command>
        </ns1:executeCommand>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
`;

const getBase64AuthString = (username, password) => Buffer.from(`${username}:${password}`).toString('base64');

const sendCommand = async (command) => {
    const soapMessage = formatSoap(command);
    const base64AuthString = getBase64AuthString(config.username, config.password);

    return fetch(`http://${config.ip}:${config.port}`, {
        method: 'POST',
        body: soapMessage,
        headers: {
            'Content-Type': 'text/xml',
            'Authorization': `Basic ${base64AuthString}`
        }
    });
} 

(async () => {
    const response = await sendCommand('notify This message was send via SOAP')
        .then(res => res.text());

    console.log(response);
})();

You will need node-fetch for this example to work (npm i node-fetch).

All 4 comments

This is neither a forum nor support. It is to report errors in the source code. Please close your "issue".

I'm just posting this because I found this on googleing:

const http = require('http')

const formatSoapBody = command => `<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
  xmlns:ns1="urn:tc"
>
  <SOAP-ENV:Body>
    <ns1:executeCommand>
      <command>.${command}</command>
    </ns1:executeCommand>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>`

const soap = command => new Promise((s, f) => {
  const req = http.request({
    hostname: 'localhost',
    port: 7878,
    method: 'POST',
    auth: `user:password`, // use correct credentials here
  }, async res => {
    console.log('wesh')
    const buf = []
    try {
      for await (const data of res) buf.push(String(data))
      if (res.statusCode !== 200) return f(Error(buf.join('').slice(380, -69)))
      s(buf.join('').slice(349, -76).replace(/&#xD;&#xA;/g, '\n'))
    } catch (err) {
      f(err)
    }
  })
  req.write(formatSoapBody(command))
  req.on('error', f)
})

const createAccount = (user, password) => soap(`account create ${user} ${password}`)
createAccount('test123', '123test')
  .then(console.log, console.error)

I found that the example above no longer works (not for me at least). So I made an updated version:

const config = {
    ip: '127.0.0.1',
    port: 7878,
    username: 'username',
    password: 'password'
}

// Everything needed to send a message via SOAP to the worldserver
const fetch = require('node-fetch');

const formatSoap = (command) => `
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:TC" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:executeCommand>
            <command>${command}</command>
        </ns1:executeCommand>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
`;

const getBase64AuthString = (username, password) => Buffer.from(`${username}:${password}`).toString('base64');

const sendCommand = async (command) => {
    const soapMessage = formatSoap(command);
    const base64AuthString = getBase64AuthString(config.username, config.password);

    return fetch(`http://${config.ip}:${config.port}`, {
        method: 'POST',
        body: soapMessage,
        headers: {
            'Content-Type': 'text/xml',
            'Authorization': `Basic ${base64AuthString}`
        }
    });
} 

(async () => {
    const response = await sendCommand('notify This message was send via SOAP')
        .then(res => res.text());

    console.log(response);
})();

You will need node-fetch for this example to work (npm i node-fetch).

anyone would like to create a page at https://trinitycore.atlassian.net/wiki/spaces/tc/overview?mode=global with some instructions ? maybe a postman collection too ? like https://www.getpostman.com/collections/0b6478ab9a896aaed851

Was this page helpful?
0 / 5 - 0 ratings

Related issues

besplash picture besplash  路  3Comments

Rushor picture Rushor  路  3Comments

daddycaddy picture daddycaddy  路  3Comments

tje3d picture tje3d  路  3Comments

Rushor picture Rushor  路  3Comments