How to download jsreport export file content

  • Hi sir,

    Right now we are importing our templates from development server to production server manually that
    means, by exporting templates from development server and manually importing to production server and also we daily download the whole templates manually from jsreport studio (development serve for backup) and store in our backup folder .This work is very hectic so instead of doing whole things manually we have read the documentation about import and export using api ie,

    var reqOpts = {
    url: jsreportUrl + '/api/import',
    method: 'POST',
    body: {},
    headers: {
    'Authorization': 'Basic ' + jsreportAuthrization

    request(reqOpts, function(err, result, body) {
        console.log(err, body)
      const fileName = 'reshma.jsrexport';
     fs.writeFile(fileName, body, (err) => { (err) ? console.log('error while writing', err) : console.log('fileis 
    generated!') });

    here i'm getting the export file response and we tried to store in a file but while uploading the same file in stuido i'm getting an error like this,


  • administrators


    i guess your code is about POST to /api/export and not to api/import (because export is the route that produces export file) right?

    what you are missing there is to pass an explicit encoding option in the request, right now your code is saving a corrupted export file because of the encoding. the body you get right now in the callback of request is string but in order to avoid any issue and corruption of data it needs to be buffer, to do that update your options to this (also avoid passing an empty body if you just want full export of the entities just don't define the body):

    var reqOpts = {
      url: jsreportUrl + "/api/export",
      method: "POST",
      encoding: null,
      headers: {
        Authorization: "Basic " + jsreportAuthrization,

  • that means convert the callback reponse to buffer before writing the response to file. isn't it?

  • administrators

    @reshmajacob93 not exactly convert the response to buffer, it is more letting it to be buffer since the very beginning, so request module does not touch it and let it in the raw format (buffer).

Log in to reply

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.