Search code examples
node.jsaxiosfilesystems

Write a file based on axios response


I am making a request to a endpoint using axios. Then, I want to get this request and save the data in a file called response.json. The file is being created, but the content is being saved like this:

[object Object]

this is my code:

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)
    });

Solution

  • What's up?

    You have to save using JSON.stringify(response.data) instead of only response.data