Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP request in Emacs

Tags:

emacs

lisp

elisp

I'm trying to send an HTTP GET request from my elisp code and store the contents of the response in a variable. As simple as

use LWP::Simple;
my $data = get("http://some.url");

I use Windows 7 & Emacs 24.2.


I tried using Emacs-Web package. Here's basically an example from the documentation, simplified even more:

(web-http-get
 (lambda (httpc header my-data)
   (message my-data))
 :url "http://breqwas.net/test.txt"))

That does not work, I get this response in minibuffer:

Keyword argument http://breqwas.net/emacs.txt not one of (:host :port :extra-headers :mode :logging)

The original piece of code from the documentation fails the same way.


I also looked at http-get function, but "get URL in a buffer" - that's not what I need. I don't need it in a buffer, I need it in a variable.

like image 425
Artyom V. Kireev Avatar asked May 08 '13 18:05

Artyom V. Kireev


1 Answers

I'd recommend using url-retrieve-synchronously that's included with Emacs. The results are placed in a buffer, but you can easily evaluate it as a string like so:

(with-current-buffer (url-retrieve-synchronously "http://stackoverflow.com")
  (prog1
      (buffer-string)
    (kill-buffer)))
like image 90
ataylor Avatar answered Sep 18 '22 11:09

ataylor