ring

Andrei Stan 2024-11-14T11:42:30.012519Z

hello, i`m having a hard time making my app redirecting/downloading a .pdf file After saving the pdf on the disk, i return a ring response with the relative path:

(ring.util.response/redirect (str pdf-location "/" pdf-name))
if i inspect in my browser "Networks": • i see status 302 (the route that handles the logic of downloading the pdf and which returns the redirect response) • status 200
scheme
	http
host
	localhost:8080
filename
	/35586426/2024/10/3772738448.pdf
I wish the browser redirects to the location :

weavejester 2024-11-16T04:58:08.820279Z

Thanks for the update

dharrigan 2024-11-14T12:20:10.379289Z

Hi, in my application, I use to allow people to download pdfs, zips and so on

dharrigan 2024-11-14T12:22:15.733539Z

bit like this

Andrei Stan 2024-11-14T12:22:45.255309Z

ok, thank you. I will read about it. i managed to make it work. i was using also HTMX in my project, and the http request were made by AJAX i replaced the htmx's hx-get with an anchor <a>

dharrigan 2024-11-14T12:22:47.403559Z

in my handler:

(defn foo-handler
   [request]
   ...
   ...
   ...
   (fn [output-stream]
     (try
       (with-open [in (s3/get-file s3 s3-key)]
         (io/copy in output-stream))
       (catch Exception e
         (log/errorf e "Unable to write to output stream for file '%s'!" s3-key))

in my route

(-> (handler/foo-handler request)
    (rio/piped-input-stream)
    (response/response)
    (response/content-type "application/zip")
    (response/header "Content-Disposition" (format "attachment; filename=%s" filename)))))))

dharrigan 2024-11-14T12:23:09.349219Z

works a treat

dharrigan 2024-11-14T12:23:46.879619Z

Good luck! 🙂

Andrei Stan 2024-11-14T12:25:53.412959Z

Thank you so much for your help, good luck as well

weavejester 2024-11-14T16:36:33.399099Z

Could you explain your issue a little more? You say that you expect it to redirect, but then you also say it returns a 302 then a 200 response - a successful redirection. Is it redirecting to the wrong URL? Is it not downloading the file? You haven't explained what's going wrong.

Andrei Stan 2024-11-15T06:17:19.033859Z

hello, @weavejester i did not used it properly. it was a PEBKAC: Problem Exists Between Keyboard And Chair 😀

Andrei Stan 2024-11-15T06:23:13.199389Z

i use HTMX in my project, i am using <a> tag which acts like a button and sends hx-get (http get) request. this request is made with ajax. i resolved my problem by using <a> with href