JsReportsOnline - any http library available?



  • I understand you don't allow custom modules for security reasons. But is any http request library available (ie. node-fetch, axios, etc)?

    How can we make calls to external APIs to bring in data?


  • administrators

    @on2air we allow the request module in jsreportonline, you can use it in script just fine.

    here is an example in a script that fetch an image and put the result of the image as base64 in data

    const request = require('request')
    
    async function beforeRender (req, res) {
        const imageUrl = req.data.imageUrl
    
        const imageBase64 = await new Promise((resolve, reject) => {
            request.get(imageUrl, {
                encoding: null
            },
            function(error, response, body) {
                if (error) { return reject(error) }
    
                if (response.statusCode === 200) {
                    resolve(Buffer.from(body).toString('base64'))
                } else {
                    reject(new Error('Got not 200 response. status code: ' + response.statusCode))
                }
            })
        })
    
        req.data.imageBase64 = imageBase64
    }
    


  • ok, thanks. although it has been deprecated as of earlier this year, so may want to explore an alternative.
    Thanks


  • administrators

    sure, we are aware of that and we plan to analyze how we are going to proceed with that, in the meantime, the library works great, it has a lot of years of maturity so there is not immediate pressure to replace it.


Log in to reply
 

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