Подтвердить что ты не робот

Как отправить XML-данные в node.js http.request

Я пытаюсь отправить xml-запрос на веб-службу через Node.js, используя http.request.

Вот мой код. Моя проблема в том, что вместо data=1 я хочу отправить xml в службу.

http.request({
   host: 'service.x.yyy.x',
   port: 80,
   path: "/a.asmx?data=1",
   method: 'POST'
}, function(resp) {
   console.log(resp.statusCode);
   if(resp.statusCode) {
        resp.on('data', function (chunk) {
            console.log(chunk);
            str +=  chunk;                  
        });
        resp.on('end', function (chunk) {                           
            console.log(str);            
        });                   
  }
}).end();

Хо сделать это?

4b9b3361

Ответ 1

http.request возвращает объект ClientRequest, который также является записываемым потоком. Вместо .end() do end(xmlbody) или .write(xmlbody).end()

Ответ 2

Фактически ссылка, предоставленная Андреем Сидовым, помогла ему работать. Это работает.

var body = '<?xml version="1.0" encoding="utf-8"?>' +
           '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
            '<soap12:Body>......</soap12:Body></soap12:Envelope>';

var postRequest = {
    host: "service.x.yyy.xa.asmx",
    path: "/a.asmx",
    port: 80,
    method: "POST",
    headers: {
        'Cookie': "cookie",
        'Content-Type': 'text/xml',
        'Content-Length': Buffer.byteLength(body)
    }
};

var buffer = "";

var req = http.request( postRequest, function( res )    {

   console.log( res.statusCode );
   var buffer = "";
   res.on( "data", function( data ) { buffer = buffer + data; } );
   res.on( "end", function( data ) { console.log( buffer ); } );

});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

req.write( body );
req.end();