Flask Download File with Get Request

35 Views Asked by At

Here is my download Function:

Testing Url = "https://youtu.be/S9-_xBxe80c?si=WM6VfX_E_aZDwusw"

Testing itag = 22
def download(url, itag):
    if not url or not itag:
        return "URL and itag parameters are required.", 400
    
    yt = YouTube(url)
    title = yt.title
    buffer = BytesIO()
    video = yt.streams.get_by_itag(itag)
    
    # video.download()
    if video:
        video.stream_to_buffer(buffer)
        buffer.seek(0)
        return send_file(
            buffer,
            as_attachment=True,
            download_name=f"{title}.mp4",
            mimetype="video/mp4",
        )
    else:
        return "Video with specified itag not found.", 404

And here is my get request:

@app.route('/download', methods=['GET'])
def download_file():
    try:
        url = request.args.get('orgUrl')
        itag = int(request.args.get('itag'))
        
        result = download(url, itag)
        return result 
        
    except KeyError:
        return "Invalid JSON format. 'orgUrl' and 'itag' fields are required.", 400
    except Exception as e:
        return str(e), 500

Bug is that when I hit the get request instead of start downloading it returns garbage buffer.

But if I use this function directly with giving static itag and url it work fine.

Please help me to figure out the issue. I am knew in Flask.

return garbadge value from get request](https://i.stack.imgur.com/GvU00.png)

0

There are 0 best solutions below