Import-export JSReport template as Folder/File



  • How to export and import JSReport data(template and scripts) through JS script, So that it can be imported to other server with that script not manually import/export fro m JSReport UI.

    Please provide info if any version control of templates stored on JSReport server.



  • You can do it with the rest call. See the API notes here
    https://jsreport.net/learn/import-export#api

    Also, you can check browser F12 tools/network and replicate the same call in js.



  • At this https://jsreport.net/learn/import-export#api, there is no more info about the api call
    The info at there is as follows:
    POST: /api/import
    BODY: /* multipart import.zip */
    Important - make sure the multipart key for uploaded zip is import.zip (Could you please explain it in more details?)

    I am using the following code to import.

    fs.createReadStream('./import.zip').on('data', function(chunk) {
    data = chunk
    // console.log(chunk.toString())
    var options = {
    url: jsreportUrl + '/api/import',
    method: 'POST',
    BODY: data,
    headers: {
    'Authorization': 'Basic ' + jsreportAuthrization,
    'Content-Type': 'multipart/form-data'
    }
    }
    request(options, function(err, result) {
    console.log(err, result)
    })
    })

    And facing Issue is as follows:
    Error: Unable to read import.zip key from multipart stream
    at multer.array (/home/ubuntu/jsreportapp/node_modules/jsreport-import-export/lib/helpers.js:124:17)
    at Array.<anonymous> (/home/ubuntu/jsreportapp/node_modules/multer/lib/make-middleware.js:53:37)
    at listener (/home/ubuntu/jsreportapp/node_modules/on-finished/index.js:169:15)
    at onFinish (/home/ubuntu/jsreportapp/node_modules/on-finished/index.js:100:5)
    at callback (/home/ubuntu/jsreportapp/node_modules/ee-first/index.js:55:10)
    at IncomingMessage.onevent (/home/ubuntu/jsreportapp/node_modules/ee-first/index.js:93:5)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

    Please respond with solution, if i have anything wrong while calling api.

    Thank you.


  • administrators

    it looks like you are not creating the request correctly, there probably is something wrong in the way that request module expects to send multipart form data. if you want some real code example of sending a request to our api/import endpint, check here and here. we use internally axios and form-data modules and the request is built correctly. i recommend that you tried the same way



  • Thanks @bjrmatos

    i looked at https://github.com/jsreport/jsreport-import-export/blob/master/cli/import.js#L226 and found the logic to call the api now it works for me.
    The i developed is as follows:

    async function startImport(zipFileToImport, jsreportUrl, jsreportAuthrization) {

        let form = new FormData();
    
        await form.append('import.zip', fs.createReadStream(zipFileToImport))
    
        const formHeaders = await new Promise((resolve, reject) => {
            form.getLength((err, length) => {
                if (err) {
                    return reject(err)
                }
    
                const headers = Object.assign({ 'Content-Length': length }, form.getHeaders())
                resolve(headers)
            })
        })
        var reqOpts = {
            url: jsreportUrl + '/api/import',
            method: 'POST',
            body: form,
            headers: {
                'Authorization': 'Basic ' + jsreportAuthrization,
                ...formHeaders
            }
        }
    
        request(reqOpts, function(err, result, body) {
            console.log(err, body)
        })
    

    }

    Thanks once again.


Log in to reply
 

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