Node.js request library -- post text/xml to body?
Well, I sort of figured it out eventually, to repost the body for a nodeJS proxy request, I have the following method:
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)
}
}
);
I get rawbody by using the following code:
app.use(function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
});
Yablargo
Fitness Enthusiast and Software Guy! I write software for a fairly large organization and am lucky enough to get to work in an environment that embraces new technology. Our Projects span pretty much every platform outthere. I catch alot of heat for this, but I still am faster/better with the .NET stack than most everything else (Node is pretty great, though!). I also perform independent consulting mostly with PHP, Node, and Django.
Updated on July 09, 2022Comments
-
Yablargo almost 2 years
I am trying to setup a simple node.js proxy to pass off a post to a web service (CSW in this isntance).
I'm posting XML in a request body, and specifying text/xml. -- The service requires these.
I get the raw xml text in the req.rawBody var and it works fine, I can't seem to resubmit it properly however.
My method looks like:
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 submit a string in a POST, using content-type text/xml. I can't seem to accomplish this however!
I am using the 'request' library @ https://github.com/mikeal/request
Edit -- Whoops! 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) } } );