本文转自:https://blog.csdn.net/dreamer2020/article/details/52074516/

在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了。有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟请求。

安装
npm install --save request
1
使用
最简单的GET请求,用法如下:

var request = require('request');
request('http://www.baidu.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the baidu homepage.
}
})

POST application/json
request({
url: url,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: JSON.stringify(requestData)
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
}
});

POST application/x-www-form-urlencoded

request.post({url:'http://service.com/upload', form:{key:'value'}}, function(error, response, body) {
if (!error && response.statusCode == 200) {
}
})

POST multipart/form-data

var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
};
request.post({url:'http://service.com/upload', formData: formData}, function (error, response, body) {
if (!error && response.statusCode == 200) {
}
})

如上所示,formData可以直接放key-value格式的数据,也可以放buffer,或者是通过流描述的文件。

参考
github request

pass JSON to HTTP POST Request
---------------------
作者:无名大盗
来源:CSDN
原文:https://blog.csdn.net/dreamer2020/article/details/52074516/
版权声明:本文为博主原创文章,转载请附上博文链接!

[转]nodejs使用request发送http请求的更多相关文章

  1. node - 使用request发送http请求

    在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了.有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟 ...

  2. request发送json-rpc请求

    直接贴代码吧: let url = '/rest/2.0/res/auth_token?session=' + session url += getUrlTokenQuery() const meth ...

  3. nodejs向远程服务器发送post请求----融云Web SDK/客户端获取token

    最近要用到一个叫融云的及时通讯的SDK,在获取token这个步骤的时候有点卡顿,以防以后碰到类似的问题,再此记录一下. 客户端通过融云 SDK 每次连接服务器时,都需要向服务器提供 Token,以便验 ...

  4. Qt 之 使用 https发送 HTTP请求(使用OPENSSL库)

    一.简述 在使用Qt发送HTTP请求中一般使用的链接都是http://前缀,而有的服务器支持 https://前缀的链接,而Qt本身是支持https的,但是https访问需要用到SSL认证,而QT默认 ...

  5. Nodejs Http发送post请求

    Nodejs  Http发送post请求 var http = require('http'); function epay(params) { console.log(" COME IN& ...

  6. Nodejs发送Post请求时出现socket hang up错误的解决办法

    参考nodejs官网发送http post请求的方法,实现了一个模拟post提交的功能.实际使用时报socket hang up错误. 后来发现是请求头设置的问题,发送选项中需要加上headers字段 ...

  7. nodejs typescript怎么发送get、post请求,如何获取网易云通信token

    nodejs typescript怎么发送get.post请求,如何获取网易云通信token yarn add jshashesyarn add superagent检查语法yarn lint==== ...

  8. nodejs模拟http发送请求

    首先需要安装模块request,然后代码如下: //模拟发送http请求 var request = require("request"); //get请求 request('ht ...

  9. 微信小程序开发 [05] wx.request发送请求和妹纸图

    1.wx.request 微信小程序中用于发起网络请求的API就是wx.request了,具体的参数太多,此处就不再一一详举了,基本使用示例如下: wx.request({ url: 'test.ph ...

随机推荐

  1. 异步使用委托delegate --- BeginInvoke和EndInvoke方法

    当我们定义一个委托的时候,一般语言运行时会自动帮委托定义BeginInvoke 和 EndInvoke两个方法,这两个方法的作用是可以异步调用委托. 方法BeginInvoke有两个参数: Async ...

  2. Java_接口与抽象类

    接口: 接口,英文interface,在java中,泛指供别人调用的方法或函数.接口是对行为的一种抽象. 语法: [public] interface InterfaceName{} 注意: 1)接口 ...

  3. MORE XOR

    MORE XOR #include<bits/stdc++.h> using namespace std; ; int a[maxn]; ][maxn]; int main() { ios ...

  4. 批量引用iconfont字体图标到项目

    打开https://www.iconfont.cn/网址登录后选择你需要的图标添加到购物车中 点击下载代码或者添加到项目后再下载代码,再找到之前下载的的文件,拷贝到项目中

  5. 12.JavaScript字符串方法

    JS字符串常用方法总结 动态方法:1.str.charAt(index);  返回子字符串,index为字符串下标,index取值范围[0,str.length-1] 动态方法:2.str.charC ...

  6. tomcat的下载与安装

    (1)下载Tomcat 官网地址:http://tomcat.apache.org/whichversion.html (2)安装Tomcat Tomcat有安装版和解压版(绿色版) 安装版以.exe ...

  7. Winsock编程基础1

    1.加载和释放Winsoke库 //所有Winsock函数都是从WS2_32.DLL导出,包含相应库文件#include <winsock2.h>#pragma comment(lib, ...

  8. monkey 命令详解

    monkey命令详解   1.  $ adb shell monkey <event-count>                <event-count>是随机发送事件数 例 ...

  9. [Swift]LeetCode391. 完美矩形 | Perfect Rectangle

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  10. [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges

    In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...