Get image with Axios request and send it to express js response

Viewed 15

I am using express js endpoint that makes a GET request with axios which outputs an image. Then send this image to response output of endpoint:

const axios = require('axios')
const token = 'Bearer key-auth'
app.get('/test', async (req, res, next) => {

    const arrayBuffer = await axios.get('example/url/image',
            { headers: { Authorization: token, responseType: "arraybuffer" }})
     res.set({
        'Content-Type': 'image/png',
        'Content-Length': arrayBuffer.data.length
      })
    res.send(arrayBuffer.data)

})

This works.. the API returns status 200. However the image returned is broken and nothing is previewd in browser except for the broken image icon: enter image description here

What am I doing wrong here?

0 Answers
Related