joaotavora / snooze

Common Lisp RESTful web development
207 stars 22 forks source link

Trouble returning pdf docs #27

Open atgreen opened 3 years ago

atgreen commented 3 years ago

I'm having trouble understanding how to return non-string content. I'm using hunchentoot, and in this case I want to return PDF documents (which are not static content). Do I need to tell the backend not to expect a string result, or is something like this supposed to work?

(snooze:defroute doc-pdf (:get :application/pdf &key id)
  ;; Load the PDF document ID as an array of octets.
  (read-document *storage-driver* id))
joaotavora commented 3 years ago

Hi @atgreen , I'd like to help, as this is a more than reasonable use case, but I'm kind of disconnected from snooze as of late. Can you supply me with a minimal example, or where to find that read-document function? Then it's much easier for me to test.

Do I need to tell the backend not to expect a string result,

In general yes, this is the current Snooze approach to this. Depending on the backend you've chosen, its practices are likely still viable inside the route's dynamic context, you can probably tell it that the return value is a sequence of octets. For example, in Hunchentoot, you can also do other stuff like setting headers in replies with (setf (hunchentoot:header-out* "Foo:") "bar"). So you can probably set some special variable to indicate that what youre about to return is octets. Ideally, you would also be able to return a stream object and the backend would deal with that.

The adaptation between snooze and the backend you've chosen is probably also a point you can tweak. Just dig around in the code.

strawhatguy commented 1 year ago

I ran into this too recently (although with images I had byte vectors of), if you're still interested @atgreen I made a PR from my fork: https://github.com/joaotavora/snooze/pull/32

strawhatguy commented 1 year ago

ah nevermind, I missed you were using hunchentoot, my PR is for clack/woo.