Making a POST with node.js

Making a POST with node.js

  1. var querystring = require(‘querystring’);  
  2. var http = require(‘http’);  
  3.   
  4. var post_domain = ‘localhost’;  
  5. var post_port = 80;  
  6. var post_path = ‘/post_demo.php’;  
  7.   
  8. var post_data = querystring.stringify({  
  9.   ‘your’ : ‘post’,  
  10.   ‘data’‘goes here!’  
  11. });  
  12.   
  13. var post_options = {  
  14.   host: post_domain,  
  15.   port: post_port,  
  16.   path: post_path,  
  17.   method: ‘POST’,  
  18.   headers: {  
  19.     ‘Content-Type’‘application/x-www-form-urlencoded’,  
  20.     ‘Content-Length’: post_data.length  
  21.   }  
  22. };  
  23.   
  24. var post_req = http.request(post_options, function(res) {  
  25.   res.setEncoding(‘utf8’);  
  26.   res.on(‘data’function (chunk) {  
  27.     console.log(‘Response: ‘ + chunk);  
  28.   });  
  29. });  
  30.   
  31. // write parameters to post body  
  32. post_req.write(post_data);  
  33. post_req.end();  
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s