help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function to download a URL and return it as a string


From: Pascal J. Bourguignon
Subject: Re: Function to download a URL and return it as a string
Date: Sun, 09 Dec 2012 14:12:29 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

Sean McAfee <eefacm@gmail.com> writes:

> I recently wanted to be able to download a document from a URL and
> return it as a string.  I couldn't find a function that did that
> precisely, but I was able to construct my own:
>
> (defun download (url)
>   (with-current-buffer (url-retrieve-synchronously url)
>     (prog2
>         (when (not (search-forward-regexp "^$" nil t))
>           (error "Unable to locate downloaded data"))
>         (buffer-substring (1+ (point)) (point-max))
>       (kill-buffer))))
>
> This seems rather busy for such a basic-seeming operation--in
> particular, having to take care to delete the buffer created by
> url-retrieve-synchronously.  Is there a better way to do it?
>
> (On the other hand, this way I get to use prog2, which I almost never
> do.)

Notice that you cannot just receive the bytes of the ressource.  At the
very least, you also need the Content-Type:.  So don't feel this header
is a bad thing.

I would have used (search-forward "\n\n"): 

(defun download (url)
  (with-current-buffer (url-retrieve-synchronously url)
     (prog1 (buffer-substring (or (search-forward "\n\n" nil t) (point-min)) 
                              (point-max))
       (kill-buffer))))
  
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]