Search code examples
javascriptsoapxmlhttprequest

Creating a SOAP XMLHttpRequest request in JavaScript


I'm trying to create a SOAP request in JavaScript, but I get a wrong response.

Here's my request:

callSOAP() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', 'https://webapi.allegro.pl/service.php', true);
    var sr = 
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<SOAP-ENV:Envelope ' + 
        'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' + 
        'xmlns:main="https://webapi.allegro.pl/service.php" ' +
        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
        'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
        '<SOAP-ENV:Body>' +
            '<main:DoGetCountriesRequest>' +
                '<main:countryCode>1</main:countryCode>' +
                '<main:webapiKey>xxxxxxxx</main:webapiKey>' +
            '</main:DoGetCountriesRequest>' +
        '</SOAP-ENV:Body>' +
    '</SOAP-ENV:Envelope>';

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.response);
        }
    };
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(sr);
}

I try to call 'DoGetCountriesRequest' method but the response is status code 500 with a message 'Invalid XML'.

Is it the proper way to call a SOAP method in JavaScript? What's wrong with my request?


Solution

  • It looks like you're sending the request to the ?wsdl endpoint - remove that from the URL in your xmlhttp.open() method call to send it to the service itself.

    It also seems your SOAP message is malformed - you've closed the SOAP-ENV:Envelope opening tag too early - it also needs to surround your xmlns:xsi and xmlns:xsd namespace definitions:

    '<?xml version="1.0" encoding="utf-8"?>' +
        '<SOAP-ENV:Envelope ' + 
            'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:main="https://webapi.allegro.pl/service.php"' +
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' + ...
    

    Follow up edit: There are some double quotes in your in your countryCode and webapiKey opening tags that need removing and it looks like the message itself doesn't comply with the WSDL - the operation doGetCountries in the WSDL needs a DoGetCountriesRequest object. Try something like:

    var sr = 
        '<?xml version="1.0" encoding="utf-8"?>' +
        '<SOAP-ENV:Envelope ' + 
            'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' + 
            'xmlns:main="https://webapi.allegro.pl/service.php" ' +
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
            '<SOAP-ENV:Body>' +
                '<main:DoGetCountriesRequest>' +
                    '<main:countryCode>1</main:countryCode>' +
                    '<main:webapiKey>xxxxxxxx</main:webapiKey>' +
                '</main:DoGetCountriesRequest>' +
            '</SOAP-ENV:Body>' +
        '</SOAP-ENV:Envelope>';