Replies: 1 comment
-
You need to transform the response data stream yourself, as Axios does this inside HTTP adapter before running the response interceptors, so you cannot change the response configuration after the response has been received. import stream from "stream";
import util from "util";
import axios from "axios";
const finished = util.promisify(stream.finished);
axios.interceptors.response.use(async (response) => {
if (
response.headers.hasContentType("application/json") &&
response.data instanceof stream.Readable
) {
let data = "";
await finished(
response.data.on("data", (chunk) => {
data += chunk.toString();
})
);
response.data = data;
response.config.responseType = 'json';
}
return response;
});
const { data, headers } = await axios.get("https://dummyjson.com/products/1", {
responseType: "stream",
});
console.log(headers, data); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
There's a couple of API's on the internet where you poll an endpoint which may be a large BLOB and may be a JSON API response.
Which means that you are now streaming every response and have to deal with JSON in user space. It would be nice to be able to switch the request config on response before the parsing.
Effectively:
However it seems like interceptors run after response handling so it's unclear how you can handle that right now.
Beta Was this translation helpful? Give feedback.
All reactions