Skip to content

Downloading private files using downloadFile method #5

@ov77

Description

@ov77

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 7.3.0
  @capacitor/core: 7.3.0
  @capacitor/android: 7.3.0
  @capacitor/ios: 7.3.0

Installed Dependencies:

  @capacitor/android: 7.3.0
  @capacitor/core: 7.3.0
  @capacitor/cli: 7.3.0
  @capacitor/ios: 7.3.0

Platform(s)

Current Behavior

In a previous version of Capacitor's Filesystem plugin, I was able to download files from private URLs using the downloadUrl function, after authenticating via cookies.

In the current version of the file-transfer capacitor, this approach no longer works. When I try to access the private URL using the exact same HTTP request, I receive a 401 Unauthorized response.

Expected Behavior

The call to download the file should send the cookie header too.

Code Reproduction

// my code using Filesystem that worked when the user is authenticated in myapp
let requestConfig: HttpOptions = {
    url: downloadApiUrl,
    method: 'get',
    connectTimeout: API_TIMEOUT_SECS * 1000,
    headers: {
        'X-USER-Api-Client': 'mobile',
        'X-USER-Session': mySessionHeader
    },
    webFetchExtra: {
        'credentials': 'include',
    }
}

const resultApi = await Filesystem.downloadFile({
    path: destPath,
    directory: destDir,
    recursive: true,
    progress: true,
    ...requestConfig
} as DownloadFileOptions);


// my new code using Filetransfer - not working when the downloadApiUrl is private and the user is authenticated in myapp
let requestConfig: HttpOptions = {
    url: downloadApiUrl,
    method: 'get',
    connectTimeout: API_TIMEOUT_SECS * 1000,
    headers: {
        'X-USER-Api-Client': 'mobile',
        'X-USER-Session': mySessionHeader
    },
    webFetchExtra: {
        'credentials': 'include',
    }
}    
    // First get the full file path using Filesystem
    const fileInfo = await Filesystem.getUri({
        directory: destDir,
        path: destPath
    });

    
    const resultApi = await FileTransfer.downloadFile({
        ...requestConfig,
        path: fileInfo.uri,
        progress: true
    }); // the http response is 401

It seems that using FileTransfer.downloadFile the request do not contains Cookie headers

Other Technical Details

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions