zweifisch / ob-http

make http request within org-mode babel
GNU General Public License v3.0
253 stars 30 forks source link
curl http-client org-babel org-mode

[[http://melpa.org/#/ob-http][file:http://melpa.org/packages/ob-http-badge.svg]] [[http://stable.melpa.org/#/ob-http][file: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.

+BEGIN_SRC emacs-lisp

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

+END_SRC

** options

| option | curl | example | |---------------+----------------+-----------------------------------------------------------------------------------------| | =:proxy= | =-x= | =:proxy localhost:8118= | | =:noproxy= | =--noproxy *= | N/A | | =:cookie-jar= | =--cookie-jar= | =:cookie-jar username= | | =:cookie= | =--cookie= | =:cookie username= | | =:max-time= | =--max-time= | default is =10= | | =:user= | =--user= | =:user admin:passwd= | =:pretty= | N/A | =:pretty= use =Content-Type=, to overwrite =:pretty json= | | =:select= | N/A | =:select path= path will be passed to [[https://stedolan.github.io/jq/][jq]] for json or [[https://github.com/EricChiang/pup][pup]] for html or [[http://xmlstar.sourceforge.net/][xmlstarlet]] for xml | | =:get-header= | N/A | =:get-header X-Subject-Token= | | =:curl= | N/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:

: 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]]