Nodejs HTTP请求库superagent的使用
Nodejs HTTP请求库superagent的使用
Superagent 是一个轻量级的、功能强大的客户端请求库,用于 Node.js 和浏览器。它支持 Promise API,并且非常易于使用。下面是一些基本用法示例:
安装
首先,你需要安装 Superagent。你可以使用 npm 或 yarn 来安装。
npm install superagent
# 或者
yarn add superagent
发送 GET 请求
const superagent = require('superagent');
superagent
.get('https://jsonplaceholder.typicode.com/posts/1')
.then(res => {
console.log(res.body); // 输出响应体
})
.catch(err => {
console.error(err);
});
发送 POST 请求
const superagent = require('superagent');
superagent
.post('https://jsonplaceholder.typicode.com/posts')
.send({ title: 'foo', body: 'bar', userId: 1 })
.then(res => {
console.log(res.body); // 输出响应体
})
.catch(err => {
console.error(err);
});
发送带有查询参数的请求
const superagent = require('superagent');
superagent
.get('https://jsonplaceholder.typicode.com/posts')
.query({ _limit: 5 })
.then(res => {
console.log(res.body); // 输出响应体
})
.catch(err => {
console.error(err);
});
处理错误
在实际应用中,你可能需要处理各种错误情况,例如网络错误、服务器错误等。你可以通过 .catch
方法来捕获这些错误。
const superagent = require('superagent');
superagent
.get('https://jsonplaceholder.typicode.com/posts')
.then(res => {
console.log(res.body);
})
.catch(err => {
if (err.status === 404) {
console.error('资源未找到');
} else if (err.status === 500) {
console.error('服务器内部错误');
} else {
console.error('未知错误:', err);
}
});
使用 Promises
如果你更喜欢使用原生 Promises 而不是回调函数,你可以直接返回 Promise:
const superagent = require('superagent');
async function fetchData() {
try {
const res = await superagent.get('https://jsonplaceholder.typicode.com/posts/1');
console.log(res.body);
} catch (err) {
console.error(err);
}
}
fetchData();
以上就是 Superagent 的一些基本用法。你可以根据需要添加更多的配置选项,如设置头部信息、超时时间等。
SuperAgent 是一个轻量级的、灵活的客户端 HTTP 库。它可以帮助你更轻松地发起 HTTP 请求。
首先,你需要安装 SuperAgent:
npm install superagent
然后你可以这样使用它:
const superagent = require('superagent');
// 发起GET请求
superagent.get('http://www.example.com')
.then(res => {
console.log(res.body);
})
.catch(err => {
console.error(err);
});
// 发起POST请求
superagent.post('http://www.example.com')
.send({ key: 'value' })
.then(res => {
console.log(res.body);
})
.catch(err => {
console.error(err);
});
这只是 SuperAgent 的冰山一角,它还有很多其他功能,如设置请求头、处理响应等。
superagent
是一个非常强大的HTTP请求库,适用于Node.js环境。它支持Promise,并且有很好的链式调用API,使得编写HTTP请求变得简单而直观。以下是一些基本的使用示例。
安装
首先,你需要安装superagent
。可以使用npm来安装:
npm install superagent
GET 请求
最简单的GET请求如下所示:
const superagent = require('superagent');
superagent.get('https://jsonplaceholder.typicode.com/posts/1')
.then(res => {
console.log(res.body);
})
.catch(err => {
console.error(err);
});
这段代码会发送一个GET请求到指定的URL,并打印返回的数据。
POST 请求
对于POST请求,你可以这样使用:
const superagent = require('superagent');
const data = { title: 'foo', body: 'bar', userId: 1 };
superagent.post('https://jsonplaceholder.typicode.com/posts')
.send(data)
.set('Content-Type', 'application/json') // 设置请求头
.then(res => {
console.log(res.body); // 打印响应体
})
.catch(err => {
console.error(err); // 打印错误
});
这段代码会向指定URL发送一个带有JSON数据的POST请求,并设置Content-Type
为application/json
。
带有查询参数的GET请求
如果你需要添加查询参数到GET请求中,可以这样做:
const superagent = require('superagent');
superagent.get('https://jsonplaceholder.typicode.com/posts')
.query({ userId: 1 }) // 添加查询参数
.then(res => {
console.log(res.body);
})
.catch(err => {
console.error(err);
});
以上就是superagent
的一些基本用法。通过这些示例,你应该能够开始使用superagent
来构建你的HTTP客户端了。
Superagent 是一个轻量级的、可扩展的客户端请求库。使用时首先需要通过npm安装:
npm install superagent
然后可以这样使用:
const superagent = require('superagent');
superagent
.get('http://www.example.com')
.end((err, res) => {
if (err) {
console.error(err);
} else {
console.log(res.body);
}
});
它支持Promise,也可以使用async/await风格:
const res = await superagent.get('http://www.example.com');
console.log(res.body);
可以方便地设置请求头、处理响应等。