Skip to content

Instantly share code, notes, and snippets.

@wobh
Created August 13, 2015 07:20
Show Gist options
  • Save wobh/20057b3e50031d8e71c9 to your computer and use it in GitHub Desktop.
Save wobh/20057b3e50031d8e71c9 to your computer and use it in GitHub Desktop.
http-utils.el
(require 'json)
(require 'url)
(require 'url-http)
;;; http utilities
(defun http-utils-response-json (url)
"Retrieve url and parse json response."
(with-current-buffer (url-retrieve-synchronously url t)
(setf (point) url-http-end-of-headers)
(json-read)))
(defun http-utils-request-visit (url)
"Retrieve url and open response in other window."
(with-current-buffer (url-retrieve-synchronously url t)
(switch-to-buffer-other-window (current-buffer))))
;;; TODO: look into just using `url-http' to make http requests
(defun http-utils-make-basic-auth (username password)
"Make basic auth header."
(cons "Authorization"
(concat "Basic "
(base64-encode-string
(concat username ":" password)))))
;;; TODO: look into secrets lookup.
(defun http-utils-request-make-form-data (alist)
(url-encode-url
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
alist
"&")))
(provide 'http-utils)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment