From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Function to download a URL and return it as a string Date: Sun, 09 Dec 2012 14:12:29 +0100 Organization: Informatimago Message-ID: <87vccb8j42.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1355058912 28929 80.91.229.3 (9 Dec 2012 13:15:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 9 Dec 2012 13:15:12 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 09 14:15:26 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Thgj6-0005xg-0a for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 14:15:24 +0100 Original-Received: from localhost ([::1]:50721 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Thgit-0006ex-Ce for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 08:15:11 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 36 Original-X-Trace: individual.net VAVsdKUbv3dKX9eWT7Lh3QMqK9bzShwUfia4T5EdCeAvxtSPwG0U4tFHe+jMg4RtcF Cancel-Lock: sha1:ZWFjMDA2OTFhNzNhNzcxOTk1NjZiNjBjOWY4MmU0MmU3MjI2YjA4Mg== sha1:lmekaedj+OsPhAIU+Z4x7dJj2G4= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:195784 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88102 Archived-At: Sean McAfee 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 {}.