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?
javascript json axios
add a comment |
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?
javascript json axios
have you setresponseType
totext
(default isjson
)?
– 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
add a comment |
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?
javascript json axios
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
javascript json axios
asked Dec 7 '16 at 8:56
LuLeBe
14528
14528
have you setresponseType
totext
(default isjson
)?
– 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
add a comment |
have you setresponseType
totext
(default isjson
)?
– 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
add a comment |
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
});
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
add a comment |
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*/});
add a comment |
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
});
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
add a comment |
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
});
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
add a comment |
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
});
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
});
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
add a comment |
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
add a comment |
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*/});
add a comment |
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*/});
add a comment |
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*/});
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*/});
answered Dec 7 '16 at 11:09
LuLeBe
14528
14528
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
have you set
responseType
totext
(default isjson
)?– 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