[转]nodejs使用request发送http请求
本文转自: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请求的更多相关文章
- node - 使用request发送http请求
在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了.有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟 ...
- request发送json-rpc请求
直接贴代码吧: let url = '/rest/2.0/res/auth_token?session=' + session url += getUrlTokenQuery() const meth ...
- nodejs向远程服务器发送post请求----融云Web SDK/客户端获取token
最近要用到一个叫融云的及时通讯的SDK,在获取token这个步骤的时候有点卡顿,以防以后碰到类似的问题,再此记录一下. 客户端通过融云 SDK 每次连接服务器时,都需要向服务器提供 Token,以便验 ...
- Qt 之 使用 https发送 HTTP请求(使用OPENSSL库)
一.简述 在使用Qt发送HTTP请求中一般使用的链接都是http://前缀,而有的服务器支持 https://前缀的链接,而Qt本身是支持https的,但是https访问需要用到SSL认证,而QT默认 ...
- Nodejs Http发送post请求
Nodejs Http发送post请求 var http = require('http'); function epay(params) { console.log(" COME IN& ...
- Nodejs发送Post请求时出现socket hang up错误的解决办法
参考nodejs官网发送http post请求的方法,实现了一个模拟post提交的功能.实际使用时报socket hang up错误. 后来发现是请求头设置的问题,发送选项中需要加上headers字段 ...
- nodejs typescript怎么发送get、post请求,如何获取网易云通信token
nodejs typescript怎么发送get.post请求,如何获取网易云通信token yarn add jshashesyarn add superagent检查语法yarn lint==== ...
- nodejs模拟http发送请求
首先需要安装模块request,然后代码如下: //模拟发送http请求 var request = require("request"); //get请求 request('ht ...
- 微信小程序开发 [05] wx.request发送请求和妹纸图
1.wx.request 微信小程序中用于发起网络请求的API就是wx.request了,具体的参数太多,此处就不再一一详举了,基本使用示例如下: wx.request({ url: 'test.ph ...
随机推荐
- python爬虫第三天
DebugLog实战 有时候我们需要在程序运行时,一边运行一边打印调试日志.此时需要开启DebugLog. 如何开启: 首先将debugleve ...
- Linux了解知识点
Linux知识点 1.linux系统内核最早由芬兰大学生linus Torvalds开发. 2.Linux主要用于服务器端和嵌入式两个领域. 3.Linux的特点:开放性.多用户.多任务.良好的用 ...
- sqlmap Windows 安装教程
第一步:下载 python :https://www.python.org/downloads/ (这里有python各种版本,但是一般建议安装3和2.7) sqlmap:https://git ...
- Redis线程模型
Redis 基于 Reactor 模式开发了自己的网络事件处理器: 这个处理器被称为文件事件处理器(file event handler): 文件事件处理器使用 I/O 多路复用(multiplexi ...
- Java的几种设计模式
java的设计模式大体上分为三大类: 创建型模式(5种):工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式. 结构型模式(7种):适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模 ...
- [Swift]LeetCode126. 单词接龙 II | Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- [Swift]LeetCode435. 无重叠区间 | Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...