Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  139] [ 2]  / answers: 1 / hits: 20198  / 8 Years ago, wed, december 7, 2016, 12:00:00

I have a react application in which I want the user to be able to upload code files that he can then view.


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 therefore not available as a String, but as a javascript object. Turning them 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?


More From » json

 Answers
0

LuleBes answer didnt work for me. What did work was:
transformResponse: (res) => { 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
});

[#59792] Sunday, December 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
annie

Total Points: 483
Total Questions: 97
Total Answers: 107

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;