Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
        }
    }
);
like image 928
Yablargo Avatar asked Oct 14 '25 14:10

Yablargo


1 Answers

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();
  });
});
like image 103
Yablargo Avatar answered Oct 18 '25 01:10

Yablargo