SuperAgent
1. 简介
SuperAgent是一个轻量级、灵活的、易读的、低学习曲线的客户端请求代理模块,使用在NodeJS环境中。
使用的示例:
var request = require('superagent')request.post('/api/pet').send({ name: 'Manny', species: 'cat' }).set('X-API-Key', 'foobar').set('Accept', 'application/json').then(res => {alert('yay got ' + JSON.stringify(res.body));});
2. 请求
2.1 请求的方法
请求的方法类型(DELETE,HEAD,PATCH,POST和PUT)可以通过如下字符串来指定:
1. 通过requst 函数方式
通过request 函数执行,传递请求参数与请求地址
语法
request('GET', '/search')参数:
- 第一个参数是请求方式: “GET”
- 第二个参数是请求URL: “/search”
// 指定使用GET方法request('GET', '/search').then(function (response) {// success}, function (error) {// failure});
2. 通过请求方法
每一个请求都可以作为方法调用, 参数为请求地址
语法:
request.get("/search")
request.get('/api/pet').then(res => {alert('yay got ' + JSON.stringify(res.body));});
除了上述使用的GET方法外,也可以使用DELETE,HEAD,PATCH,POST和PUT方法,只需要简单地更改一下方法的名称。
3. 特殊请求方法
在旧版本IE浏览器中delete是系统的关键字,为了兼容这一点,可以调用del()方法来避免冲突。
request.del('/book/1').end(function(response){});
4. 简写方式
request默认使用GET方法发送请求,可以简写成如下:
request('/search', (error, response) => {});
2.2 发送请求获取数据
通过调用request对象then()或end()方法,或使用await关键字,发送一个请求。
1. 通过then 方法发送请求
通过then方法发送请求,并获取数据, then方法接受两个参数, 第一个参数是成功后的回调函数, 第二个是失败后的回调函数, 第二个参数也可以使用catch方法处理
request.get("https://cnodejs.org/").then(data => {console.log(data)}, error => {console.log(error)})
catch 方法
request.get("https://cnodejs.org/").then(data => {console.log(data)}).catch( error => {console.log(error)})
2. 通过end 方法发送请求
end 方法接受一个函数参数, 函数有两个参数,第一个参数是接受失败的值,第二个参数接受成功的值
request.get("https://cnodejs.org/").end(function(error, response){console.log(error)console.log("-------------")console.log(response.text)console.log("-------------")})
3. 设置头部字段
报文header字段设置通过调用set()方法。
3.1 设置一个字段
没调用一次set方法,设置一个相应头
语法
request.set("请求头字段属性","请求头字段属性值")
request.get("http://localhost:3001").set("Accept","application/json").then( response => {console.log(response.text)})
这种方法如果要设置多个请求头字段需要调用多次set方法
request.get("http://localhost:3001").set('API-Key', 'foobar').set("Accept","application/json").then( response => {console.log(response.text)})
3.2 设置多个字段:
也可以传递一个对象来设置多个字段:
request.get("http://localhost:3001").set({"Accept":"application/json","API-Key": "foobar"}).then( response => {console.log(response.text)})
4. 请求参数
4.1 GET请求参数
当我们使用GET请求传递查询字符串的时候,可以使用query()方法,同时传递一个对象作为参数,
request.get("http://localhost:3001/wuwei").query({name: 'wuwei'}).query({ age: 19 }).then( response => {console.log(response.text)})
也可以将所有的get参数写在同一个对象里
request.get("http://localhost:3001/wuwei").query({name: 'wuwei',age: 20}).then( response => {console.log(response.text)})
query() 方法也可以接受字符串
request.get("http://localhost:3001/wuwei").query("name=wuwei&age=18").then( response => {console.log(response.text)})
也可以接受多个字符串, 会自动使用&符拼接的
request.get("http://localhost:3001/wuwei").query("name=wuwei").query("age=22").then( response => {console.log(response.text)})
4.2 POST 请求参数
一个典型的JSON POST请求的代码实现如下,设置相应的Content-type头字段,再写入数据。在这个例子里,仅使用JSON字符串:
request.post("http://localhost:3001/wuwei").set('Content-Type', 'application/json').send('{"name":"wuwei","age":18}').then( response => {console.log(response.text)})
