Search code examples
javascriptnode.jscanvasnode-canvas

Follow redirect on loadImage() function


I always get error "Error: Server responded with 301" when trying to use loadImage() function.

The URL i provide in the function redirected from http to https, but instead of following the redirect, the function returned that error instead.

async run(msg, { item }) {
    const canvas = Canvas.createCanvas(330, item.height);
    const ctx = canvas.getContext('2d');
    ctx.fillStyle = 'rgb(54, 57,62)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    const iconImg = await Canvas.loadImage(item.icon); // <---
    ctx.drawImage(iconImg, 0, 0);
    const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png');
    msg.channel.send(attachment);
}

Is there any way to make that function to follow redirects for 301 redirection instead of returning that error?


Solution

  • This will be fixed in the next release (https://github.com/Automattic/node-canvas/pull/1398) - 2.4.2 or 2.5.0.

    Until then you'll have to manually make the HTTP request and follow the redirects.