all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Function to download a URL and return it as a string
Date: Sun, 09 Dec 2012 14:12:29 +0100	[thread overview]
Message-ID: <87vccb8j42.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: m2k3srerd9.fsf@gmail.com

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 {}.


  parent reply	other threads:[~2012-12-09 13:12 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
2012-12-09 13:12 ` Pascal J. Bourguignon [this message]
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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vccb8j42.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.