common-lispbinary-data

How to download and save a file using drakma:http-request and flexistreams


I am trying to download and save a PDF but it fails while writing with an EOF-Error. What would be the correct way of doing this?

(with-open-file (file "/home/*/test.pdf"
                      :direction :io
                      :if-does-not-exist :create
                      :if-exists :supersede
                      :element-type '(unsigned-byte 8))
  (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                    :want-stream t)))
    (awhile (read-byte input)
      (write-byte it file))
    (close input)))

Solution

  • The solution was that I forgot to use the two optional parameters of read-byte.

    The correct way would be to set eof-error-p and eof-value to nil:

    (with-open-file (file "/home/*/test.pdf"
                          :direction :output
                          :if-does-not-exist :create
                          :if-exists :supersede
                          :element-type '(unsigned-byte 8))
      (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                        :want-stream t)))
        (awhile (read-byte input nil nil)
          (write-byte it file))
        (close input)))