Original Text
Comparison Text
1
import axios, { AxiosRequestConfig, AxiosResponse, CancelTokenSource } from 'axios';
1
import axios, { AxiosRequestConfig, AxiosResponse, CancelTokenSource } from 'axios';
2
+
import fs from 'fs';
2
// Define the request configuration
3
// Define the request configuration
3
const config: AxiosRequestConfig = {
4
const config: AxiosRequestConfig = {
4
  method: 'get',
5
  method: 'get',
5
-
  url: 'https://example.com/api',
6
+
  url: 'https://example.com/image.jpg', // Replace with the actual image URL
7
+
  responseType: 'stream', // Set the response type to 'stream' for downloading files
6
  headers: {
8
  headers: {
7
-
    'Content-Type': 'application/json',
8
    'Authorization': 'Bearer your_token_here'
9
    'Authorization': 'Bearer your_token_here'
9
  },
10
  },
10
  cancelToken: new axios.CancelToken((cancel) => {
11
  cancelToken: new axios.CancelToken((cancel) => {
11
    // Function to cancel the request
12
    // Function to cancel the request
Expand 13 lines ...
25
  .then((response: AxiosResponse) => {
26
  .then((response: AxiosResponse) => {
26
    // Handle the response data
27
    // Handle the response data
27
-
    const data = response.data;
28
+
    const writer = fs.createWriteStream('image.jpg'); // Replace with the desired file path
28
-
    // Parse the data
29
+
    response.data.pipe(writer);
29
-
    // const parsedData = JSON.parse(data);
30
  })
30
  })
31
  .catch((error) => {
31
  .catch((error) => {
32
    // Handle any errors
32
    // Handle any errors
33
    if (axios.isCancel(error)) {
33
    if (axios.isCancel(error)) {
34
      console.log('Request canceled', error.message);
34
      console.log('Request canceled', error.message);
35
    } else {
35
    } else {
Expand 6 lines ...