var http = require('http'); var querystring = require('querystring'); var postData = querystring.stringify({ 'content': '測試成功了', 'cid': 348 // 課程編號 });
var options = { hostname: 'www.imooc.com', port: 80, path: '/course/docomment', method: 'POST', headers: { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', 'Content-Length': postData.length, // 這裏須要改成postData.length 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': '此處填寫本身的cookie', 'Host': 'www.imooc.com', 'Origin': 'http://www.imooc.com', 'Referer': 'http://www.imooc.com/video/8837', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } } var req = http.request(options, function(res) { console.log('Status: ' + res.statusCode); console.log('Headers: ' + JSON.stringify(res.headers)); res.on('data', function(chunk) { console.log(Buffer.isBuffer(chunk)); console.log(typeof chunk); }); res.on('end', function() { console.log('評論完畢!'); }); }); req.on('error', function(e) { console.log('Error: ' + e.message); }); req.write(postData); req.end();