Fetch() API not updating JSON file











up vote
-1
down vote

favorite












I have a JSON file I want to read from and write to that looks like this:



[
"test@example.com"
]


I want to add info to this file using the fetch() API. So far, I can only read from this file.



let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};

fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});

fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});


I get no errors (aside from that PUT comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?










share|improve this question







New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • just to clarify - are you trying to update local file with the fetch API?
    – rufus1530
    Nov 8 at 10:35










  • Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
    – wombat trash
    Nov 8 at 10:37

















up vote
-1
down vote

favorite












I have a JSON file I want to read from and write to that looks like this:



[
"test@example.com"
]


I want to add info to this file using the fetch() API. So far, I can only read from this file.



let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};

fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});

fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});


I get no errors (aside from that PUT comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?










share|improve this question







New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • just to clarify - are you trying to update local file with the fetch API?
    – rufus1530
    Nov 8 at 10:35










  • Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
    – wombat trash
    Nov 8 at 10:37















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a JSON file I want to read from and write to that looks like this:



[
"test@example.com"
]


I want to add info to this file using the fetch() API. So far, I can only read from this file.



let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};

fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});

fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});


I get no errors (aside from that PUT comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?










share|improve this question







New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a JSON file I want to read from and write to that looks like this:



[
"test@example.com"
]


I want to add info to this file using the fetch() API. So far, I can only read from this file.



let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};

fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});

fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});


I get no errors (aside from that PUT comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?







javascript fetch-api






share|improve this question







New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 10:28









wombat trash

104




104




New contributor




wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






wombat trash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • just to clarify - are you trying to update local file with the fetch API?
    – rufus1530
    Nov 8 at 10:35










  • Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
    – wombat trash
    Nov 8 at 10:37




















  • just to clarify - are you trying to update local file with the fetch API?
    – rufus1530
    Nov 8 at 10:35










  • Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
    – wombat trash
    Nov 8 at 10:37


















just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35




just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35












Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37






Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










fetch() is an API for making HTTP requests.



It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/!)



If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    wombat trash is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205841%2ffetch-api-not-updating-json-file%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    fetch() is an API for making HTTP requests.



    It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/!)



    If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.






    share|improve this answer

























      up vote
      3
      down vote



      accepted










      fetch() is an API for making HTTP requests.



      It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/!)



      If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.






      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        fetch() is an API for making HTTP requests.



        It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/!)



        If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.






        share|improve this answer












        fetch() is an API for making HTTP requests.



        It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/!)



        If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 10:32









        Quentin

        631k718501018




        631k718501018






















            wombat trash is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            wombat trash is a new contributor. Be nice, and check out our Code of Conduct.













            wombat trash is a new contributor. Be nice, and check out our Code of Conduct.












            wombat trash is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205841%2ffetch-api-not-updating-json-file%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)