0

My client posted data from one website to my website using npm request module.

ie as follows.

       testservice : function(req , res){

       var data = { title : 'my title' , content : 'my content'};

       request.post('https://dev.example.com/test' , data , function(err , response ,body){

        if (err) console.log(err);
        if(response) console.log('statuscode='+response.statuscode);

       });
       };

I tried to get the JSON data posted to my site from my client's site using request get method , but i didnt get json data output.

Please help me out to get JSON data which is posted using request post method. Thanks.

Mahahari
  • 901
  • 1
  • 16
  • 35

2 Answers2

1

Try this:

testservice: function(req, res) {
  var data = { title: 'my title', content: 'my content' },
      options = {
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      };

  request.post('https://dev.example.com/test', options, function(err, response, body) {
    if (err) console.log(err);
    if (response) console.log('statuscode=' + response.statuscode);
  });
};
mscdex
  • 93,083
  • 13
  • 170
  • 135
0

I tried to get the JSON data posted to my site from my client's site using request get method, but i didnt get json data output.

I believe you may be misunderstanding the request.get function. It doesn't "get" the data that was posted to your site, it in fact fires a "get" request off to a particular URL.

If you want to receive data on your site that was POST'ed, then you need to configure your server to listen for POST requests from your friends site and then parse out the posted data from the body of that request.

i.e. in your server code if you're using raw node.js

http.createServer(function(req,res){
    if(req.method.toUpperCase() === "POST"){
        //code to parse out the data from the post request
    }
}).listen(8080)

For more detailed info on parsing out the POST'ed data, see How do you extract POST data in Node.js?

Let me know if this helps, please clarify your question if not.

Community
  • 1
  • 1
pooley1994
  • 446
  • 3
  • 15
  • hi we are not using raw node.js , we are using sails.js ie node .js aand express is there..how can i do this in sails,js? – Mahahari Aug 08 '14 at 03:24
  • In express you would use app.post("/path/posted/to",function(req,res){ //code to parse the data out}) as documented here... http://expressjs.com/4x/api.html#app.VERB – pooley1994 Aug 08 '14 at 03:33