Search code examples
node.jspostxmlhttprequestexpress

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.

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

Ho to do this?


Solution

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