From: "Drew Adams" <drew.adams@oracle.com>
To: "'Sean McAfee'" <eefacm@gmail.com>, <help-gnu-emacs@gnu.org>
Subject: RE: Function to download a URL and return it as a string
Date: Sat, 8 Dec 2012 21:38:15 -0800 [thread overview]
Message-ID: <48DA15B7852B46D1A879470B55F3AC5A@us.oracle.com> (raw)
In-Reply-To: <m2k3srerd9.fsf@gmail.com>
> 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?
Dunno anything about whether there is a better way, but consider using
`unwind-protect', with `kill-buffer' as your cleanup part. And to play safe,
pass the actual buffer (returned from `url-*') as arg to `kill-buffer'.
Using `unwind-protect' makes sure the buffer is killed when you're done, no
matter what.
next prev parent reply other threads:[~2012-12-09 5:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-09 5:17 Function to download a URL and return it as a string Sean McAfee
2012-12-09 5:38 ` Drew Adams [this message]
2012-12-09 13:12 ` Pascal J. Bourguignon
2012-12-09 15:13 ` jun net
2012-12-09 19:26 ` Jambunathan K
[not found] ` <mailman.14907.1355031511.855.help-gnu-emacs@gnu.org>
2012-12-10 0:59 ` Sean McAfee
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=48DA15B7852B46D1A879470B55F3AC5A@us.oracle.com \
--to=drew.adams@oracle.com \
--cc=eefacm@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).