how to post XML data in node.js http.request

i am trying to submit a xml request to a web service via Node.js using http.request.

Here is my code. My issue is that instead of data=1 i want to post xml to the service.

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

Ho to do this?


http.request returns ClientRequest object which is also a writable stream. Instead of .end() do end(xmlbody) or .write(xmlbody).end()

Actually the link given by Andrey Sidorov helped to get it working. This works.

var body = '<?xml version="1.0" encoding="utf-8"?>' +
           '<soap12:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap12="">'+

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 );

Need Your Help

Traversing a n-ary tree without using recurrsion

algorithm tree-traversal

How can I traverse an n-ary tree without using recursion?