0
curl -X POST -d @test.XML https://example.com/example

What is the restful equivalent of the above curl command, where test.XML is the XML file. Or How could we replicate this request in nodejs?

vishnuk
  • 79
  • 9

1 Answers1

0

The answer is based on this thread.

var xmlbody;

function processFile() {

    var postReq = {
        host: 'example.com',
        port: 443,
        path: '/example',
        method: 'POST',
        headers: {
            'Cookie': 'cookie',
            'Content-Type': 'text/xml',
            'Content-Length': Buffer.byteLength(xmlbody)
        }
    };

    var req = require('http').request(postReq, function(resp) {
        var str;
        if(resp.statusCode) {
            resp.on('data', function (chunk) { str +=  chunk; });
            resp.on('end', function (chunk) {});                   
        }
    });

    req.write(xmlbody);
    req.end(); 
}

require('fs').readFile('@test.XML', function read(err, data) {
    xmlbody = data;
    processFile();
});

Also, below is my server side test code.

var express = require('express');
var app = express();
var xmlparser = require('express-xml-bodyparser');
app.use(xmlparser());
app.post('/', function(req, res){
    console.log(req.body);
    return res.end();
});    
app.listen(/* port */)

Example output on server side from w3c:

{ note: 
   { to: [ 'Tove' ],
     from: [ 'Jani' ],
     heading: [ 'Reminder' ],
     body: [ 'Don\'t forget me this weekend!' ] } }
Community
  • 1
  • 1
Kir Chou
  • 2,980
  • 1
  • 36
  • 48
  • i tried , i got this error events.js:141 throw er; // Unhandled 'error' event ^ Error: read ECONNRESET at exports._errnoException (util.js:870:11) at TCP.onread (net.js:552:26) – vishnuk Oct 03 '16 at 09:50
  • That's because your serverlet is not able to access. Please try my example server code. (Or you can provide your serverlet code) – Kir Chou Oct 03 '16 at 10:23