Запрос SOAP в NodeJs возвращает 500 Internal Server Error

В данный момент я работаю над небольшим проектом, где мне нужно сделать запрос SOAP к хост-службе управления SAP PCo.

Я работал над доказательством концепции и создал документ javascript на стороне клиента, который отлично работал. Поэтому я попытался реализовать этот скрипт в NodeJs на стороне сервера. Для этого я создал веб-сервер с помощью node-express и сделал запрос с помощью node-request, но если я отправлю запрос в службы хоста управления PCo, ответ 500 Internal Server Error.

Хост управления PCo отображает необработанный HTTP-запрос для целей тестирования, который выглядит следующим образом:

POST LINKTOHOST HTTP/1.1
User-Agent: PCo
SOAPAction: urn:sap.com:pco.management/PCoManagementService/GetAgentInstancesRuntimeInfo
Authorization: Basic ********
Content-Type: text/xml
Host: HOST
Content-Length: 300
Expect: 100-continue
Accept-Encoding: gzip, deflate

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:urns="urn:sap.com:pco.management"><soap:Body><urns:GetAgentInstancesRuntimeInfo xsi:nil="true" /></soap:Body></soap:Envelope>

Я взял этот необработанный запрос и создал свой собственный. Вот мое рабочее доказательство концепции javascript:

var userName = prompt("Please Log in with your Windows User Account. Username:","");
var userPW  = prompt("Passwort: ", "");

reqAllAgentRuntimeInfo();
setInterval(function() {reqAllAgentRuntimeInfo()}, 5000);

 /*
 *
 *  Functions
 * 
 */ 

//Sends out the SOAP request via XMLHttpRequest
function reqAllAgentRuntimeInfo()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', 'LINKTOHOST', true, userName, userPW);

    var soapRequest =
    '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:urns="urn:sap.com:pco.management">' +
        '<soap:Body>' +
            '<urns:GetAgentInstancesRuntimeInfo xsi:nil="true" />' +
        '</soap:Body>'+
    '</soap:Envelope>';

    xmlhttp.setRequestHeader('SOAPAction', 'urn:sap.com:pco.management/PCoManagementService/GetAgentInstancesRuntimeInfo');
    xmlhttp.setRequestHeader('Authorization', 'Basic');
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    
    xmlhttp.send(soapRequest);

    xmlhttp.onreadystatechange = function()
    {
        //Test for Status Code 4 & 200 = OK
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            displayData(xmlhttp);
        }
    };
}

А затем я создал этот скрипт NodeJs на стороне сервера, который не работает, и я не могу объяснить себе, почему.

const http = require('http'); // 1 - Import Node.js  modules
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 50001;

app.use(bodyParser.urlencoded({ extended: true}));

app.get('/', (req, res) => 
{
    res.redirect('/logIn');
});

app.get('/logIn', (req, res) => 
{
    res.sendFile(__dirname + '/sites/logIn.html');
});

app.post('/logIn', (req, res) =>
{
    //Encode User Credentials
    var base64url = require('base64url');
    var credentials = 'Basic ' + base64url(req.body.userName + ':' + req.body.password);
    
    var request = require('request');
    let soapReq = 
    '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:urns="urn:sap.com:pco.management">' +
        '<soap:Body>' +
            '<urns:GetAgentInstancesRuntimeInfo xsi:nil="true" />' +
        '</soap:Body>'+
    '</soap:Envelope>';

    var options = 
    {
        url: 'LINKTOHOST',
        method: 'POST',
        body: soapReq,
        headers:
        {
            'Host': 'HOST',
            'Content-Length':soapReq.length,
            'Accept-Encoding': 'gzip,deflate',
            'User-Agent': 'PCo',
            'SOAPAction': "urn:sap.com:pco.management/PCoManagementService/GetAgentInstancesRuntimeInfo",
            'Authorization': credentials,
            'Content-Type': "text/xml"
        }
    };
    
    let callback = (error, response, body) => 
    {
        if(!error && response.statusCode == 200){
            console.log('PCo Communication raw result', body);
        }
        else
        {
            console.log('PCo Communication Error: ', response.statusCode, response.statusMessage);
        }
    }
    console.log(options);
    request(options, callback);
})

app.listen(port, () =>
{
    console.log(`Webserver listening at http://localhost:${port}`);
}); 

Я был бы рад, если бы кто-то мог мне помочь.

Спасибо.


person JamesRyan    schedule 14.08.2020    source источник


Ответы (2)


особая благодарность factorypolaris. Теперь это работает. Вот мой код:

let soapReq = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:urns="urn:sap.com:pco.management">' +
        '<soap:Body>' +
            '<urns:GetAgentInstancesRuntimeInfo xsi:nil="true" />' +
        '</soap:Body>'+
    '</soap:Envelope>';

    var options = 
    {
        host: '127.0.0.1',
        auth: req.body.userName + ':' + req.body.password,
        method: 'POST',
        port: 55555,
        path: '/PCoManagement',
        //body: soapReq, -->*Remove this*
        headers:
        {
            'SOAPAction': "urn:sap.com:pco.management/PCoManagementService/GetAgentInstancesRuntimeInfo",
            'Content-Type': "text/xml"
        }
    };
    
    var req = http.request(options, function (response) {
        console.log('STATUS: ' + response.statusCode);
        console.log('HEADERS: ' + JSON.stringify(response.headers));
        response.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        });
    });
    
    req.write(soapReq); //--> Add this to write your actual data to the message

    req.end();  //--> Add end() to define the end of your request
person JamesRyan    schedule 17.08.2020

Ваш запрос ({options}) устарел

{options}, которые вы используете для request(), больше не актуальны. Более новые версии request() берут на себя большую часть тяжелой работы за вас. См. изменения, описанные ниже.

// REMOVE Immediate line break after =
let soapReq = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:urns="urn:sap.com:pco.management">' +
        '<soap:Body>' +
            '<urns:GetAgentInstancesRuntimeInfo xsi:nil="true" />' +
        '</soap:Body>'+
    '</soap:Envelope>';

В NODE назначение переменных не может начинаться с разрыва строки.

// This will cause problems.
let x = 
''+'y';


// Do it like this is OK.
let x = ''+
'y';

var options = 
    {
        hostname: 'LINKTOHOST', // KEY NAME CHANGE: url -> hostname
        auth: 'user:password', //  Moved (No base64URLEncode())
        method: 'POST',
        body: soapReq,
        headers:
        {
            // 'Host': 'HOST', ---> REMOVE
            // 'Authorization': credentials, ---> Moved To Above             
            'Content-Length':soapReq.length, // May not be needed
            'Accept-Encoding': 'gzip,deflate', // May not be need
            'User-Agent': 'PCo',
            'SOAPAction': "urn:sap.com:pco.management/PCoManagementService/GetAgentInstancesRuntimeInfo",
            'Content-Type': "text/xml"
        } 
    };

request(options, function (response) {
                console.log('STATUS: ' + response.statusCode);
                console.log('HEADERS: ' + JSON.stringify(response.headers));
                response.on('data', function (chunk) {
                    console.log('BODY: ' + chunk);
                });
            });

*******
SEE The Commented Lines for edits.
*******

См. современный пример

person factorypolaris    schedule 14.08.2020
comment
Спасибо за помощь. Итак, я внес изменения в свой код, но теперь функция обратного вызова не работает. В нем говорится следующее: TypeError: Не удается прочитать свойство 'statusCode' из неопределенного. Если я попытаюсь вывести значение в консоль, он скажет undefined. - person JamesRyan; 14.08.2020
comment
Итак, теперь есть еще одна ошибка. STATUS: undefined HEADERS: undefined TypeError: response.on is not a function - person JamesRyan; 14.08.2020
comment
Убедитесь, что options.headers.host не определен. Попробуйте изменить options.hostname либо на URL, либо на хост. Убедитесь, что вы можете получить доступ к URL-адресу другими способами, чтобы убедиться, что проблема не на другом конце. - person factorypolaris; 14.08.2020
comment
Я думаю, вам следует проверить правильность используемого вами модуля. Node V10+ имеет встроенный запрос через модуль http. Взгляните сюда, и если вы используете достаточно новую версию узла, внесите изменения. Все, что у вас есть сейчас, должно быть совместимо. См. здесь: https://nodejs.org/api/http.html#http_http_request_options_callback - person factorypolaris; 14.08.2020