silkimen / cordova-plugin-advanced-http

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
400 stars 321 forks source link

feat: response object on file download #451

Closed h4ck-rOOt closed 2 years ago

h4ck-rOOt commented 2 years ago

As a user i may need access header or other meta information sent by the server. So i added the response object as second parameter to the success callback when triggered by downloadFile(...) or sendRequest(..., { method: 'download', ... }, ...) to make sure the feature maintains backward compatibility.

The response object will look like this:

{
    file: { ... }, // raw file object
    data: FileEntry, // exactly the same as first parameter
    headers: { ... }, // Header collection in form of key->value
    status: 200, // Status Code
    url: 'https://basictest.url'
}

You are now able to access the header information like this:

cordova.plugin.http.downloadFile(
    "https://www.google.de",
    {}, 
    {}, 
    cordova.file.dataDirectory + 'test.html', 
    function(entry, response) { 
        const cType = response.headers['content-type'];
        const aProto = response.headers['x-android-selected-protocol'];
        // ...
    },
    console.error
);