2012-12-24 17 views
14

我正在嘗試使用http.request通過Node.js向Web服務提交一個xml請求。如何在node.js中發佈XML數據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(); 

to要做到這一點?

回答

4

http.request返回ClientRequest對象,它也是一個可寫的流。 而不是.end()end(xmlbody).write(xmlbody).end()

20

實際Andrey Sidorov給出的鏈接幫助,使其能工作。 這有效。

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

我這樣做,但它顯示「使用POST方法發送'xml'參數」。我做的事 – vinodh