4

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)
        }
    }
);
Yablargo
  • 3,520
  • 7
  • 37
  • 58
  • Can you let us know what version of express you are using.. because req.rawBody was [removed](https://github.com/senchalabs/connect/commit/c3170eee8cd60c92bcccca6054c1ebbb93a1a821#diff-0) in the latest version – Sriharsha Sep 27 '13 at 21:54
  • Using the snippet @ http://stackoverflow.com/questions/18710225/node-js-get-raw-request-body-using-express/18710277#18710277 – Yablargo Sep 30 '13 at 04:14

1 Answers1

8

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
  • 3,520
  • 7
  • 37
  • 58