Report downloading issue

Hi,

I'm trying to download reports in local machine using a node js application. But I'm getting empty data in the downloaded file. Report getting downloaded with data when running the downloadUrl in browser. Response status code is 303. can someone help on this issue.

let targetDirectory = 'D:/';
let fileName = name;
let ext = 'csv';
var sourceURL = downloadUrl;

request.get(sourceURL, function (response) 
{
    const file = fs.createWriteStream((targetDirectory + fileName + '.' + ext));
    response.pipe(file);
    file.on('finish',() => {
        file.close();
        console.log('Download Completed');
    })

@tim.smith @Jerome.Saint-Marc Please have a look on this.

Thanks,

Hello,

The downloading of a report is in 2 steps (i.e. 2 HTTP requests).
The first request to the reportUrl must have an Authentication header with a Genesys Cloud bearer/access token.
If successful, you will get a 303 HTTP Response (redirect) with the url to S3 to use to download the file (as Location header of the 303 HTTP Response).
The second request (to S3) must not have the Authentication header defined.

Note that you must use the same user/server context than the one used to generate the report.
I mean that you need to use a Genesys Cloud access token related to the same user/server context.

Assuming you used 'request' npm to make your HTTP requests, here is an example (not optimized - you may have to clean this code a bit):

let targetDirectory = 'D:/';
let fileName = name;
let ext = 'csv';
var sourceURL = downloadUrl;
var accessToken = genesysAccessToken;

request.get({
    url: sourceURL,
    headers: {
        'Authorization': 'Bearer ' + accessToken
    }
}, function (error, response, body) {
    // In case of 303 Redirect, the url to download the file is available in the HTTP Response Location header
    // You can access it using response.request.href
    console.log('URL to access the file: ', response.request.href);

    let file = fs.createWriteStream(targetDirectory + fileName + '.' + ext);
    let stream = request({
        uri: response.request.href,
        headers: {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
        },
        gzip: false
    })
    .pipe(file)
    .on('finish', () => {
        file.close();
        console.log(`The file has been downloaded.`);
    })
    .on('error', (error) => {
        console.log(`Something wrong happened: ${error}`);
    })
});

Regards,

It was a great help. Thanks Jerome. It worked.

One more thing I would like to know, Is it possible to download the file directly to Amazon S3 bucket?

That's more a question for Amazon :slight_smile:
On Genesys Cloud side, no, the file is available for download via url.

But I don't think the AWS S3 SDK allows to copy a file from a url into an S3 bucket.
I haven't used that SDK for a while so I don't know if this has changed.
I remember there was a way to pipe the stream from a url download towards the S3 bucket.

Regards,

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.