-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathhttp.js
41 lines (36 loc) · 1.21 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import axios from 'axios';
export default (fastify, options, done) => {
// 读取 views 目录下的 encoder.html 文件并返回
fastify.post('/http', async (req, reply) => {
const {url, headers = {}, params = {}, method = 'GET', data = {}} = req.body;
if (!url) {
return reply.status(400).send({error: 'Missing required field: url'});
}
try {
const response = await axios({
url,
method,
headers,
params,
data,
});
reply.status(response.status).send({
status: response.status,
headers: response.headers,
data: response.data,
});
} catch (error) {
if (error.response) {
// 服务器返回了非 2xx 状态码
reply.status(error.response.status).send({
error: error.response.data,
status: error.response.status,
});
} else {
// 请求失败或其他错误
reply.status(500).send({error: error.message});
}
}
});
done();
};