Create a download from a http url

Hey everyone,

I’m currently working on creating a function that leverages a website to generate a proxy for downloading video files. The function works perfectly fine locally, but I’m encountering an issue when I push the site. The error message I’m getting is:

error decoding lambda response: error decoding lambda response: json: cannot unmarshal object into Go struct field .body of type string

I’ve been troubleshooting this issue, but I haven’t been able to pinpoint the root cause. If anyone has experience with this type of error or has suggestions on how to resolve it, I would greatly appreciate your input.

Here is my code :

const axios = require('axios');
const stream = require('stream');

const handler = async (event) => {
  const id = event.queryStringParameters && event.queryStringParameters.id;
  const name = event.queryStringParameters && event.queryStringParameters.name;
  const ext = event.queryStringParameters && event.queryStringParameters.ext;

  try {
    const response = await axios({
      method: 'get',
      url: `http://mol-2.com:8080/${id}.${ext}`,
      responseType: 'stream',
    });

    // Create a readable stream from the Axios response stream
    const videoStream = response.data;

    return {
      statusCode: 200,
      headers: {
        'Content-Disposition': `attachment; filename="${name}.${ext}"`,
        'Content-Type': 'video/mp4', // Adjust the MIME type based on the actual video format
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': '0',
      },
      body: videoStream.pipe(new stream.PassThrough()), // Use a PassThrough stream for direct transmission
      isBase64Encoded: false,
    };
  } catch (error) {
    console.error('Erreur lors du transfert de la vidéo :', error);

    return {
      statusCode: 500,
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ error: 'Erreur lors du transfert de la vidéo.', details: error.message }),
    };
  }
};

module.exports = { handler };

Thanks in advance!

Streaming response needs additional config: Lambda compatibility for Functions | Netlify Docs OR switch to Netlify Functions API v2.