1

I've known the way to send a simple HTTP request using Node.js as the following:

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: '/foo.html'
};

http.get(options, function(resp){
  resp.on('data', function(chunk){
    //do something with chunk
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});

I want to know how to embed parameters in the body of POST request and how to capture them from the receiver module.

1
  • 1
    if you are new to this, don't even try to accomplish this with http code module instead more friendly frameworks like express or other modules. Commented Mar 6, 2014 at 12:15

2 Answers 2

5

Would you mind using the request library. Sending a post request becomes as simple as

var options = {
url: 'https://someurl.com',
'method': 'POST',
 'body': {"key":"val"} 

};

 request(options,function(error,response,body){
   //do what you want with this callback functon
});

The request library also has a shortcut for post in request.post method in which you pass the url to make a post request to along with the data to send to that url.

Edit based on comment

To "capture" a post request it would be best if you used some kind of framework. Since express is the most popular one I will give an example of express. In case you are not familiar with express I suggest reading a getting started guide by the author himself.

All you need to do is create a post route and the callback function will contain the data that is posted to that url

app.post('/name-of-route',function(req,res){
 console.log(req.body);
//req.body contains the post data that you posted to the url 
 });
Sign up to request clarification or add additional context in comments.

1 Comment

That's is great but how to capture these body parameter in the receiver side ?
0

If you want to use the native http module, parameters can be included in body this way:

var http = require('follow-redirects').http;
var fs = require('fs');

var options = {
  'method': 'POST',
  'hostname': 'example.com',
  'path': '/foo.html',
  'headers': {
  },
  'maxRedirects': 20
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

var postData = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"examplekey\"\r\n\r\nexamplevalue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--";
req.setHeader('content-type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
req.write(postData);
req.end();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.