axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Is it possible to return the response data as a string?

Open davidatthepark opened this issue 7 years ago • 6 comments

mock.onGet('/api').reply(200, '{"test": 5}')

response.data is a JS object that looks like {test: 5} instead of a string.

Is there any way to force the response to be a string?

davidatthepark avatar Jun 23 '18 01:06 davidatthepark

i'd also like to use an array instead of an object. The second parameter of reply should be any type

jeroensak avatar Nov 23 '18 09:11 jeroensak

@ctimmerm any update or fix on how to send an array as a data? I am a bit struggling with it.

mohit-pandey avatar Sep 11 '19 07:09 mohit-pandey

nothing news?

simon25608 avatar Apr 10 '20 08:04 simon25608

The axios default transformResponse parses strings to json objects.

If you don't want this to happen, replace transformResponse in the config.

joebowbeer avatar May 24 '20 09:05 joebowbeer

try this mock.onGet().replyOnce((config) => { config.transformResponse = [function (data) { return JSON.stringify(data) }] return [ 200, {test: 5} ] })

sdauxing avatar Sep 17 '20 05:09 sdauxing

try this mock.onGet().replyOnce((config) => { config.transformResponse = [function (data) { return JSON.stringify(data) }] return [ 200, {test: 5} ] })

This won't work if the transformResponse is the thing you want to test.

At the moment, it appears a custom transformResponse on the request is called, but the data isn't passed to it. I'm going to investigate further.

jamespearson avatar Aug 23 '22 11:08 jamespearson