make http request within org-mode babel
Clone or download
legoscia and zweifisch Quote all arguments in command history buffer (#35)
With this change, you can copy a command from the history buffer and
paste it in the terminal without having to quote any arguments
manually.  For example, before this change, a "Content-Type" header
would appear like this:

    -H Content-Type: application/json

and after the change like this:

    -H Content-Type\:\ application/json
Latest commit b1428ea Jul 7, 2018

README.org

ob-http

http://melpa.org/packages/ob-http-badge.svg http://stable.melpa.org/packages/ob-http-badge.svg

http request in org-mode babel, requires curl

#+BEGIN_SRC http :pretty
GET https://api.github.com/repos/zweifisch/ob-http/languages
Accept: application/vnd.github.moondragon+json
#+END_SRC

#+RESULTS:
: {
:   "Emacs Lisp": 8170
: }

setup

To use ob-http in an org-babel source block, the http language must be enabled in the custom org-babel-load-languages alist. Alternatively, running the following snippet during initialization will enable the mode.

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (http . t)))

options

optioncurlexample
:proxy-x:proxy localhost:8118
:noproxy--noproxy *N/A
:cookie-jar--cookie-jar:cookie-jar username
:cookie--cookie:cookie username
:max-time--max-timedefault is 10
:user--user:user admin:passwd
:prettyN/A:pretty use Content-Type, to overwrite :pretty json
:selectN/A:select path path will be passed to jq for json or pup for html or xmlstarlet for xml
:get-headerN/A:get-header X-Subject-Token
:curlN/A:curl --insecure --compressed additional arguments for curl
:resolve--resolve:resolve example.com:80:127.0.0.1,example.com:443:127.0.0.1

examples

set arbitrary header

#+BEGIN_SRC http :pretty
GET http://httpbin.org/user-agent
User-Agent: ob-http
#+END_SRC

#+RESULTS:
: {
:   "user-agent": "ob-http"
: }

json

#+BEGIN_SRC http :pretty
POST http://httpbin.org/post
Content-Type: application/json

{
  "key": "value"
}
#+END_SRC

#+RESULTS:
#+begin_example
{
  "url": "http://httpbin.org/post",
  "json": {
    "key": "value"
  },
  "headers": {
    "User-Agent": "curl/7.35.0",
    "Host": "httpbin.org",
    "Content-Type": "application/json",
    "Content-Length": "18",
    "Accept": "*/*"
  },
  "form": {},
  "files": {},
  "data": "{  \"key\": \"value\"}",
  "args": {}
}
#+end_example

form submit

#+BEGIN_SRC http :pretty
PATCH http://httpbin.org/patch

key=value&foo=value
#+END_SRC

#+RESULTS:
#+begin_example
{
  "url": "http://httpbin.org/patch",
  "origin": "116.227.144.38",
  "json": null,
  "headers": {
    "User-Agent": "curl/7.35.0",
    "Host": "httpbin.org",
    "Content-Type": "application/x-www-form-urlencoded",
    "Content-Length": "19",
    "Accept": "*/*"
  },
  "form": {
    "key": "value",
    "foo": "value"
  },
  "files": {},
  "data": "",
  "args": {}
}
#+end_example

variable

#+HEADER: :var name="ob-http"
#+HEADER: :var password="secret"
#+BEGIN_SRC http :select .json
POST http://httpbin.org/post
Content-Type: application/json

{
    "auth": {
        "name": "${name}",
        "password": "${password}"
    }
}
#+END_SRC

#+RESULTS:
: {
:   "auth": {
:     "password": "secret",
:     "name": "ob-http"
:   }
: }

use properties

supported headers:

  • pretty
  • proxy
  • noproxy
  • cookie
  • schema
  • host
  • port
  • user
  • max-time
* api test
:PROPERTIES:
:header-args: :var token="secret" :host httpbin.org :pretty
:END:

#+BEGIN_SRC http
POST /post
Content-Type: application/json
X-Auth-Token: ${token}
#+END_SRC

#+RESULTS:
#+begin_example
{
  "url": "http://httpbin.org/post",
  "json": null,
  "headers": {
    "X-Auth-Token": "secret",
    "User-Agent": "curl/7.35.0",
    "Host": "httpbin.org",
    "Content-Type": "application/json",
    "Accept": "*/*"
  },
  "form": {},
  "files": {},
  "data": "",
  "args": {}
}
#+end_example

files

#+BEGIN_SRC http :file zweifisch.jpeg
GET https://avatars.githubusercontent.com/u/447862?v=3
#+END_SRC
#+RESULTS:
[[file:zweifisch.jpeg]]