Node.js request library - send text / xml to body?

I am trying to configure a simple node.js proxy to pass a message to a web service (CSW in this isntance).

I send the XML to the request body and specify text / xml. - Service requires them.

I get raw XML text in var.rawBody var and it works fine, but I cannot resend it.

My method looks like this:

app.post('/csw*', function(req, res){


  console.log("Making request to:"  + geobusOptions.host + "With query params: " + req.rawBody);


request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    'Content-Type': 'text/xml'
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
});

I just want to send a string to POST using text like text / xml. I can't seem to do this!

I use the library 'request' @ https://github.com/mikeal/request

Change - Oops! I forgot to just add the headers ...

This works great:

request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    headers: {'Content-Type': 'text/xml'}
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
+4
source share
1 answer

, , , nodeJS proxy, :

request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    headers: {'Content-Type': 'text/xml'}
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

rawbody, :

app.use(function(req, res, next) {
  req.rawBody = '';
  req.setEncoding('utf8');

  req.on('data', function(chunk) { 
    req.rawBody += chunk;
  });

  req.on('end', function() {
    next();
  });
});
+6

All Articles