Disable JSON parsing in Axios











up vote
5
down vote

favorite












Ich have a react application in which I want the user to be able to upload code files which he can then take a look at on the site.
So naturally, .json files are also accepted. Now to get the file contents, I use axios to make a get request to the file on the server.



This works fine for everything except JSON files, which are automatically parsed and therefor not available as a String, but as a javascript object. Turning then into a string again with JSON.stringify removes all line breaks, so I can't do that.



Is there any way to stop axios from automatically parsing JSON?










share|improve this question






















  • have you set responseType to text(default is json)?
    – Vedran Jukic
    Dec 7 '16 at 9:04










  • @VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
    – LuLeBe
    Dec 7 '16 at 9:08















up vote
5
down vote

favorite












Ich have a react application in which I want the user to be able to upload code files which he can then take a look at on the site.
So naturally, .json files are also accepted. Now to get the file contents, I use axios to make a get request to the file on the server.



This works fine for everything except JSON files, which are automatically parsed and therefor not available as a String, but as a javascript object. Turning then into a string again with JSON.stringify removes all line breaks, so I can't do that.



Is there any way to stop axios from automatically parsing JSON?










share|improve this question






















  • have you set responseType to text(default is json)?
    – Vedran Jukic
    Dec 7 '16 at 9:04










  • @VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
    – LuLeBe
    Dec 7 '16 at 9:08













up vote
5
down vote

favorite









up vote
5
down vote

favorite











Ich have a react application in which I want the user to be able to upload code files which he can then take a look at on the site.
So naturally, .json files are also accepted. Now to get the file contents, I use axios to make a get request to the file on the server.



This works fine for everything except JSON files, which are automatically parsed and therefor not available as a String, but as a javascript object. Turning then into a string again with JSON.stringify removes all line breaks, so I can't do that.



Is there any way to stop axios from automatically parsing JSON?










share|improve this question













Ich have a react application in which I want the user to be able to upload code files which he can then take a look at on the site.
So naturally, .json files are also accepted. Now to get the file contents, I use axios to make a get request to the file on the server.



This works fine for everything except JSON files, which are automatically parsed and therefor not available as a String, but as a javascript object. Turning then into a string again with JSON.stringify removes all line breaks, so I can't do that.



Is there any way to stop axios from automatically parsing JSON?







javascript json axios






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 7 '16 at 8:56









LuLeBe

14528




14528












  • have you set responseType to text(default is json)?
    – Vedran Jukic
    Dec 7 '16 at 9:04










  • @VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
    – LuLeBe
    Dec 7 '16 at 9:08


















  • have you set responseType to text(default is json)?
    – Vedran Jukic
    Dec 7 '16 at 9:04










  • @VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
    – LuLeBe
    Dec 7 '16 at 9:08
















have you set responseType to text(default is json)?
– Vedran Jukic
Dec 7 '16 at 9:04




have you set responseType to text(default is json)?
– Vedran Jukic
Dec 7 '16 at 9:04












@VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
– LuLeBe
Dec 7 '16 at 9:08




@VedranJukic I've tried that already, but no change. I suppose it's because of the content-type set by the server, but I'd like to solve this on the client instead of changing my api
– LuLeBe
Dec 7 '16 at 9:08












2 Answers
2






active

oldest

votes

















up vote
4
down vote













LuleBes answer didnt work for me. What did work was:
transformResponse: (req) => { return res; },
As in:



    axios.get(url, {
headers,
transformResponse: (res) => {
// Do your own parsing here if needed ie JSON.parse(res);
return res;
},
responseType: 'json'
}).then(response => {
// response.data is an unparsed string
});





share|improve this answer























  • How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
    – LuLeBe
    Aug 7 '17 at 16:46










  • ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
    – user3711421
    Aug 8 '17 at 8:48


















up vote
0
down vote













Ok I figured out how that would work. You can disable response processing by just passing the transformResponse Array in the config, which is then used instead of the the default. There you just provide an empty array or an array of functions you need to apply to your response, like this:



axios.get(URL, {transformResponse: })
.then(response => {/*response.data is plain text*/});





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


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41013082%2fdisable-json-parsing-in-axios%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    LuleBes answer didnt work for me. What did work was:
    transformResponse: (req) => { return res; },
    As in:



        axios.get(url, {
    headers,
    transformResponse: (res) => {
    // Do your own parsing here if needed ie JSON.parse(res);
    return res;
    },
    responseType: 'json'
    }).then(response => {
    // response.data is an unparsed string
    });





    share|improve this answer























    • How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
      – LuLeBe
      Aug 7 '17 at 16:46










    • ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
      – user3711421
      Aug 8 '17 at 8:48















    up vote
    4
    down vote













    LuleBes answer didnt work for me. What did work was:
    transformResponse: (req) => { return res; },
    As in:



        axios.get(url, {
    headers,
    transformResponse: (res) => {
    // Do your own parsing here if needed ie JSON.parse(res);
    return res;
    },
    responseType: 'json'
    }).then(response => {
    // response.data is an unparsed string
    });





    share|improve this answer























    • How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
      – LuLeBe
      Aug 7 '17 at 16:46










    • ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
      – user3711421
      Aug 8 '17 at 8:48













    up vote
    4
    down vote










    up vote
    4
    down vote









    LuleBes answer didnt work for me. What did work was:
    transformResponse: (req) => { return res; },
    As in:



        axios.get(url, {
    headers,
    transformResponse: (res) => {
    // Do your own parsing here if needed ie JSON.parse(res);
    return res;
    },
    responseType: 'json'
    }).then(response => {
    // response.data is an unparsed string
    });





    share|improve this answer














    LuleBes answer didnt work for me. What did work was:
    transformResponse: (req) => { return res; },
    As in:



        axios.get(url, {
    headers,
    transformResponse: (res) => {
    // Do your own parsing here if needed ie JSON.parse(res);
    return res;
    },
    responseType: 'json'
    }).then(response => {
    // response.data is an unparsed string
    });






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 13:01









    Peracek

    433514




    433514










    answered Aug 7 '17 at 13:50









    user3711421

    4291719




    4291719












    • How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
      – LuLeBe
      Aug 7 '17 at 16:46










    • ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
      – user3711421
      Aug 8 '17 at 8:48


















    • How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
      – LuLeBe
      Aug 7 '17 at 16:46










    • ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
      – user3711421
      Aug 8 '17 at 8:48
















    How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
    – LuLeBe
    Aug 7 '17 at 16:46




    How does it help that response.data is an empty object? I want the file contents, not an empty object. I don't understand what you're doing here, seeing that your transform function takes the data and returns only an empty string.
    – LuLeBe
    Aug 7 '17 at 16:46












    ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
    – user3711421
    Aug 8 '17 at 8:48




    ops, forgot to change it from my testing in my app. Now the answer should reflect the question.
    – user3711421
    Aug 8 '17 at 8:48












    up vote
    0
    down vote













    Ok I figured out how that would work. You can disable response processing by just passing the transformResponse Array in the config, which is then used instead of the the default. There you just provide an empty array or an array of functions you need to apply to your response, like this:



    axios.get(URL, {transformResponse: })
    .then(response => {/*response.data is plain text*/});





    share|improve this answer

























      up vote
      0
      down vote













      Ok I figured out how that would work. You can disable response processing by just passing the transformResponse Array in the config, which is then used instead of the the default. There you just provide an empty array or an array of functions you need to apply to your response, like this:



      axios.get(URL, {transformResponse: })
      .then(response => {/*response.data is plain text*/});





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Ok I figured out how that would work. You can disable response processing by just passing the transformResponse Array in the config, which is then used instead of the the default. There you just provide an empty array or an array of functions you need to apply to your response, like this:



        axios.get(URL, {transformResponse: })
        .then(response => {/*response.data is plain text*/});





        share|improve this answer












        Ok I figured out how that would work. You can disable response processing by just passing the transformResponse Array in the config, which is then used instead of the the default. There you just provide an empty array or an array of functions you need to apply to your response, like this:



        axios.get(URL, {transformResponse: })
        .then(response => {/*response.data is plain text*/});






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 7 '16 at 11:09









        LuLeBe

        14528




        14528






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41013082%2fdisable-json-parsing-in-axios%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff