Old question, yes... But in the event that anyone Googles this; another option is using Org Babel and ob-http
... (Can be installed from melpa.)
With org-babel
, you can make an .org
file containing "http
" code blocks. When these blocks are evaluated, they will make the HTTP request and return the response as the result.
If these blocks have a "#+NAME:
" attribute, you can use their results in any other org-babel
block. This allows for some pretty neat literate programming using the results of HTTP requests.
For example, here is a little Org document demonstrating making an HTTP request and parsing the returned JSON in Ruby:
* The request
The following is the example request shown on [[https://github.com/zweifisch/ob-http][ob-http's Github page]]...
It isn't anything fancy, but it is a REST API request, and returns
JSON, so it works for the sake of this demonstration:
#+NAME:ob-languages
#+BEGIN_SRC http :pretty
GET https://api.github.com/repos/zweifisch/ob-http/languages
#+END_SRC
#+RESULTS:
: {
: "Emacs Lisp": 7034
: }
Notice how this has a "=#+NAME:=" attribute? This is a name we can use
in other =org-babel= blocks. (As you will see below)
* Using the request
Now that I have an =http= request in an org block with a name... Lets
write something in a completely different language and use our HTTP
request's response:
#+BEGIN_SRC ruby :var langs=ob-languages
require 'json'
JSON.parse(langs)['Emacs Lisp']
#+END_SRC
#+RESULTS:
: 7034
The =:var= keyword allowed me to assign the "=langs=" variable in the
Ruby block to the result of the =ob-languages= block [[The request][above]].
This didn't have to be in Ruby, this could have been any language,
including another =http= block.
This is what this looks like in org-mode
:
Hitting C-c C-c
on the bottom block (The Ruby one) will automagically evaluate the top one for its dependency (That's the :var
bit in the block's header.). This will mean that the http
request is made first, and then the results are passed to Ruby for further processing.
You can do this with as many blocks as you like and with as many languages.
If this suits your needs, ob-http
requires a little bit of manual tweaking after you install it to get it working. (Don't worry, it isn't that much)
After installing ob-http
, you need to customize two variables: org-src-lang-modes
, and org-babel-load-languages
.
So, by running M-x customize-variable
, you can customize each to include the following:
org-src-lang-modes
:
You will want to customize this variable to include one more language mapping, so you can insert one more value to the end of the list:
String: http
Symbol: ob-http
Then you can C-x C-s
to save that customization.
org-babel-load-languages
:
You will want to add one more item to the list of enabled org
languages. "http
".
Though, you might need to add it to the list manually if the option is not in the possible options, you can do that by clicking "State" and choosing ":
" to show the Lisp expression... Then you would add the following just before the last closing parenthesis:
(http . t)
After that, a quick C-x C-s
and M-x org-reload
should be all you need..!