node.js以post请求方式发送http请求

var http = require(‘http‘);
var querystring = require(‘querystring‘);
// 请求参数
var postData = querystring.stringify({
    ‘name‘: "wangbin",
    ‘password‘: "123456",
    ‘serverId‘: 2
});
var options = {
  hostname: ‘192.168.1.135‘,
  port: 3001,
  path: ‘/login‘,
  method: ‘POST‘,
  headers: {
    ‘Content-Type‘: ‘application/x-www-form-urlencoded‘,
    ‘Content-Length‘: postData.length
  }
};
var req = http.request(options, function(res) {
  res.setEncoding(‘utf8‘);
  var resData = [];
  res.on(‘data‘, function (chunk) {
    resData.push(chunk);
  });
  res.on(‘end‘, function() {
    var data = resData.join("");
    console.log(data);
  })
});
req.on(‘error‘, function(e) {
  console.log(‘problem with request: ‘ + e.message);
});
// 发送请求
req.write(postData);
req.end();
时间: 2024-07-30 23:27:40

node.js以post请求方式发送http请求的相关文章

Ubuntu14.04 搭建 node.js 环境(Binaries方式)

从官网下载 http://nodejs.org/download/ Linux Binaries (.tar.gz)  下载下来的是node-v0.10.29-linux-x64.tar.gz文件 解压 $ tar zxvf node-v0.10.29-linux-x64.tar.gz /etc/profile.d/下建立 node.sh文件 export NODEHOME=/home/username/Software/node #指定你的解压后Binaries文件路径 export PATH

python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/[email protected]/blog/static/132229655201231085444250/ 测试用CGI,名字为test.py,放在apache的cgi-bin目录下:#!/usr/bin/pythonimport cgidef main():     print "Content-t

node.js中文件读取方式中流方式

1.node.js中文件读取流方式: var fs=require("fs"); var data; var s1=fs.createReadStream("input.txt"); s1.on("data",function(txt){ data+=txt; }) s1.on("end",function(){ console.log("文件流式读取成功!") }) 2.管道方式: var fs=requ

在Node.js使用Promise的方式操作Mysql

最近在学习Node.js,虽然早就听说了回调地狱结果过了一周就遇到了.所以花时间学习了了一下Promise.虽然还有Async/await.co.生成器等选择,但是因为本人基础较差,以及时间问题所以决定先用好Promise. 你可以选择用原生的,当然最好还是用BlueBird,听说性能比官方的好很多,而且有额外的特性:promisifyAll.Promisify 官方案例: var fs = Promise.promisifyAll(require("fs")); fs.readFil

android中Post方式发送HTTP请求

Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setDoInput(true);//表示从服务器获取数据 c

Vue--axios:vue中的ajax异步请求(发送和请求数据)

一.使用axios发送get请求 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv=&q

Vue--axios:vue中的ajax异步请求(发送和请求数据)、vue-resource异步请求和跨域

跨域原理: 一.使用axios发送get请求 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-eq

jquery的get方式发送AJAX请求

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content='text/html;charset=utf-8'> 5 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" ></script> 6 <title>&

jquery的post方式发送AJAX请求

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content='text/html;charset=utf-8'> 5 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" ></script> 6 <title>&