wkh237 / react-native-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.61k stars 1.59k forks source link

Android download manager fetch causes cookies to not be used #604

Open Noitidart opened 6 years ago

Noitidart commented 6 years ago

Android download manager is failing to download, even though download succeeds without download manager.

The above gives error:

"Error: Download manager failed to download from https://myseti.net/api/files/25844492-f7b7-4dcb-a9f7-4d65678ce6c7?version=original. Statu Code = 16"

https://github.com/wkh237/react-native-fetch-blob/blob/5f3c018b0a2b11246d1cce5b41f20634a2cf2d85/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java#L651

However this code works fine:

            const res = await RNFetchBlob.config({
                fileCache: true,
                appendExt: 'png'
            })
            .fetch('GET', 'https://myseti.net/api/files/25844492-f7b7-4dcb-a9f7-4d65678ce6c7?version=original');

But this does not use download manager. :(

Noitidart commented 6 years ago

Is it possible the cookies from the regular fetch call is not being used by the download manager? Is there a way to make it use the cookies? I tried this but it didnt help:

        const res = await RNFetchBlob.config(....)
        .fetch('GET', originalUrl, {
            'Set-Cookie': '................' // i added cookie here
        });
Noitidart commented 6 years ago

Here are the notifications that show after the above fails:

That one on the bottom with custom notification title is because i used the .config options to set a custom notif title.

Noitidart commented 6 years ago

Yes this was the problem. I had to do a dummy request, then set Cookie header in order for it to work:

    // dummy request to figure out cookie
    const resCookie = await fetchApi('session');
    if (resCookie.status !== 200) return alert('Failed to download, you must not be logged in anymore.');
    const cookie = resCookie.headers.map['set-cookie'][0];

    // download it
    const res = await RNFetchBlob.config({
        fileCache: true,
        addAndroidDownloads: {
            useDownloadManager: true,
            mime: mimeType,
            description
        }
    })
    .fetch('GET', originalUrl, { 'Cookie':cookie });

If i don't use the download manager, then the normal cookies used by normal fetch are also used by RNFetchBlob.fetch.

Ariel08081214 commented 6 years ago

any progress?