SuperAgent

1. 简介

SuperAgent是一个轻量级、灵活的、易读的、低学习曲线的客户端请求代理模块,使用在NodeJS环境中。
使用的示例:

  1. var request = require('superagent')
  2. request
  3. .post('/api/pet')
  4. .send({ name: 'Manny', species: 'cat' })
  5. .set('X-API-Key', 'foobar')
  6. .set('Accept', 'application/json')
  7. .then(res => {
  8. alert('yay got ' + JSON.stringify(res.body));
  9. });

2. 请求

2.1 请求的方法

请求的方法类型(DELETE,HEAD,PATCH,POST和PUT)可以通过如下字符串来指定:

1. 通过requst 函数方式

通过request 函数执行,传递请求参数与请求地址

语法

  1. request('GET', '/search')

参数:

  1. 第一个参数是请求方式: “GET”
  2. 第二个参数是请求URL: “/search”
  1. // 指定使用GET方法
  2. request('GET', '/search')
  3. .then(function (response) {
  4. // success
  5. }, function (error) {
  6. // failure
  7. });
2. 通过请求方法

每一个请求都可以作为方法调用, 参数为请求地址

语法:

  1. request.get("/search")
  1. request
  2. .get('/api/pet')
  3. .then(res => {
  4. alert('yay got ' + JSON.stringify(res.body));
  5. });

除了上述使用的GET方法外,也可以使用DELETE,HEAD,PATCH,POST和PUT方法,只需要简单地更改一下方法的名称。

3. 特殊请求方法

在旧版本IE浏览器中delete是系统的关键字,为了兼容这一点,可以调用del()方法来避免冲突。

  1. request
  2. .del('/book/1')
  3. .end(function(response){
  4. });
4. 简写方式

request默认使用GET方法发送请求,可以简写成如下:

  1. request('/search', (error, response) => {
  2. });
2.2 发送请求获取数据

通过调用request对象then()或end()方法,或使用await关键字,发送一个请求。

1. 通过then 方法发送请求

通过then方法发送请求,并获取数据, then方法接受两个参数, 第一个参数是成功后的回调函数, 第二个是失败后的回调函数, 第二个参数也可以使用catch方法处理

  1. request
  2. .get("https://cnodejs.org/")
  3. .then(data => {
  4. console.log(data)
  5. }, error => {
  6. console.log(error)
  7. })

catch 方法

  1. request
  2. .get("https://cnodejs.org/")
  3. .then(data => {
  4. console.log(data)
  5. })
  6. .catch( error => {
  7. console.log(error)
  8. })
2. 通过end 方法发送请求

end 方法接受一个函数参数, 函数有两个参数,第一个参数是接受失败的值,第二个参数接受成功的值

  1. request
  2. .get("https://cnodejs.org/")
  3. .end(function(error, response){
  4. console.log(error)
  5. console.log("-------------")
  6. console.log(response.text)
  7. console.log("-------------")
  8. })

3. 设置头部字段

报文header字段设置通过调用set()方法。

3.1 设置一个字段

没调用一次set方法,设置一个相应头

语法

  1. request.set("请求头字段属性","请求头字段属性值")
  1. request
  2. .get("http://localhost:3001")
  3. .set("Accept","application/json")
  4. .then( response => {
  5. console.log(response.text)
  6. })

这种方法如果要设置多个请求头字段需要调用多次set方法

  1. request
  2. .get("http://localhost:3001")
  3. .set('API-Key', 'foobar')
  4. .set("Accept","application/json")
  5. .then( response => {
  6. console.log(response.text)
  7. })
3.2 设置多个字段:

也可以传递一个对象来设置多个字段:

  1. request
  2. .get("http://localhost:3001")
  3. .set({
  4. "Accept":"application/json",
  5. "API-Key": "foobar"
  6. })
  7. .then( response => {
  8. console.log(response.text)
  9. })

4. 请求参数

4.1 GET请求参数

当我们使用GET请求传递查询字符串的时候,可以使用query()方法,同时传递一个对象作为参数,

  1. request
  2. .get("http://localhost:3001/wuwei")
  3. .query({name: 'wuwei'})
  4. .query({ age: 19 })
  5. .then( response => {
  6. console.log(response.text)
  7. })

也可以将所有的get参数写在同一个对象里

  1. request
  2. .get("http://localhost:3001/wuwei")
  3. .query({
  4. name: 'wuwei',
  5. age: 20
  6. })
  7. .then( response => {
  8. console.log(response.text)
  9. })

query() 方法也可以接受字符串

  1. request
  2. .get("http://localhost:3001/wuwei")
  3. .query("name=wuwei&age=18")
  4. .then( response => {
  5. console.log(response.text)
  6. })

也可以接受多个字符串, 会自动使用&符拼接的

  1. request
  2. .get("http://localhost:3001/wuwei")
  3. .query("name=wuwei")
  4. .query("age=22")
  5. .then( response => {
  6. console.log(response.text)
  7. })
4.2 POST 请求参数

一个典型的JSON POST请求的代码实现如下,设置相应的Content-type头字段,再写入数据。在这个例子里,仅使用JSON字符串:

  1. request
  2. .post("http://localhost:3001/wuwei")
  3. .set('Content-Type', 'application/json')
  4. .send('{"name":"wuwei","age":18}')
  5. .then( response => {
  6. console.log(response.text)
  7. })