PricingBlog

How to initiate a download from toddle?

  • notarsmatso-1207379424601964644

    Christian

    2 years ago

    Hello.
    I'm calling a post request which returns a file (successfully), but it does not show up as a download in the browser, but just resides in the response body.

    What must be done to initiate the download in the browser?

    Thanks!
  • stockton_f-1207381312915374120

    Stockton

    2 years ago

    👋 Welcome!

    Do you control the file download part? IE, when the request is made from toddle to the server and the server responds, can you control what the response looks like?

    I'll have to check but I'm pretty sure it has to do with the response headers and/or returning a "blob".
  • notarsmatso-1207383078146809897

    Christian

    2 years ago

    Yes! But I am fairly new to this. When calling it in postman and downloading a PDF it shows up correctly. In my server I return a readableStream
    1207383077567991808-image.png
    1207383077916246137-image.png
  • stockton_f-1207385785452466196

    Stockton

    2 years ago

    aww right, well I'm a bit out of my depth here.

    I would definitely check the headers in the request and the response. Maybe the stream doesn't work in browser and it's just the blob you need to return.

    Sorry not much more help.
  • notarsmatso-1207389256843001857

    Christian

    2 years ago

    Thanks I will try it out tommorow!
  • stockton_f-1207414305465700382

    Stockton

    2 years ago

    I currently do this in order to return a JSON file back to the browser. Works in Toddle when I send a GET request to my endpoint.
    1207414305230688256-Greaterthanmarketinggmail.coms_Account_Cloudflare_2024-02-14_at_12.52.43_PM.jpg
  • notarsmatso-1207616762229559377

    Christian

    2 years ago

    I am using deno (via supabase) which recommends using a stream. https://docs.deno.com/runtime/tutorials/file_server and it seems like in my case it should be the correct approach. @Erik Beuschau @Andreas Møller Do you guys have any idea on how I can trigger the download in Toddle?
  • Tod-1207616763710275604

    Tod

    2 years ago

    Great energy @Christian! Your continuous contribution to the toddle Community just made you advance to Community Level 1!
  • andreasmoller-1207618205905068042

    Andreas Møller

    2 years ago

    You cant download a file through the API interface today. Usually the best solution is to simply use a link <a href="/url/to/file" download>
  • andreasmoller-1207618558746697748

    Andreas Møller

    2 years ago

    That requires that the request is a GET request that does not require any special headers.

    If that does not work for your specific use-case you will need to write a custom action.
  • notarsmatso-1207625060802895902

    Christian

    2 years ago

    Thanks