根据axios响应写一个文件
Write a file based on axios response
我正在使用 axios 向端点发出请求。
然后,我想获取此请求并将数据保存在名为 response.json 的文件中。
正在创建文件,但内容是这样保存的:
[object Object]
这是我的代码:
const axios = require('axios');
const filesystem = require('fs');
// Make request using to the api using axios
axios.get('http://date.jsontest.com/')
.then(response => {
// console.log(response.data);
filesystem.writeFile('response.json', response.data, function (err) {
console.log(err);
});
})
.catch(err => {
console.log(err)
});
怎么了?
您必须使用 JSON.stringify(response.data)
而不是仅 response.data
来保存
我正在使用 axios 向端点发出请求。 然后,我想获取此请求并将数据保存在名为 response.json 的文件中。 正在创建文件,但内容是这样保存的:
[object Object]
这是我的代码:
const axios = require('axios');
const filesystem = require('fs');
// Make request using to the api using axios
axios.get('http://date.jsontest.com/')
.then(response => {
// console.log(response.data);
filesystem.writeFile('response.json', response.data, function (err) {
console.log(err);
});
})
.catch(err => {
console.log(err)
});
怎么了?
您必须使用 JSON.stringify(response.data)
而不是仅 response.data