码迷,mamicode.com
首页 > Web开发 > 详细

nodejs post请求

时间:2017-05-11 19:43:02      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:ati   for   ror   length   problem   encoding   world   utf8   ica   

const http = require(‘http‘);
const querystring = require(‘querystring‘);

const postData = querystring.stringify({
  ‘msg‘: ‘Hello World! ----- 哈利路亚‘
});

const options = {
  hostname: ‘192.168.1.6‘,
  port: 8080,
  path: ‘/hello‘,
  method: ‘POST‘,
  headers: {
    ‘Content-Type‘: ‘application/x-www-form-urlencoded‘,
    ‘Content-Length‘: Buffer.byteLength(postData)
  }
};

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding(‘utf8‘);
  res.on(‘data‘, (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on(‘end‘, () => {
    console.log(‘No more data in response.‘);
  });
});

req.on(‘error‘, (e) => {
  console.error(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();

 

nodejs post请求

标签:ati   for   ror   length   problem   encoding   world   utf8   ica   

原文地址:http://www.cnblogs.com/thankyouGod/p/6842083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!