From f54e5ba11e5cc2c41c85ea79b80916c400b1c75f Mon Sep 17 00:00:00 2001 From: David Morgan Date: Fri, 16 Aug 2024 12:20:18 +0100 Subject: Add restclient-copy-curl-command --- .emacs.d/lisp/init-misc.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to '.emacs.d/lisp/init-misc.el') diff --git a/.emacs.d/lisp/init-misc.el b/.emacs.d/lisp/init-misc.el index d71c646..86ec023 100644 --- a/.emacs.d/lisp/init-misc.el +++ b/.emacs.d/lisp/init-misc.el @@ -44,6 +44,25 @@ (cdar restclient-saved-requests) (alist-get (completing-read "Call: " (map-keys restclient-saved-requests)) restclient-saved-requests nil nil 'string-equal)))) (apply 'restclient-http-do args)))) + + ;; https://github.com/pashky/restclient.el/issues/288#issuecomment-1775770753 + (defun my/restclient-copy-curl-command () + "Formats the request as a curl command and copies the command to the clipboard." + (interactive) + (restclient-http-parse-current-and-do + '(lambda (method url headers entity) + (let* ((header-args + (apply 'append + (mapcar (lambda (header) + (list "-H" (format "\"%s: %s\"" (car header) (cdr header)))) + headers))) + (header-parsed (mapconcat 'identity header-args " ")) + (method-arg (concat "-X" " " method)) + (entity-arg (if (> 0 (string-width entity)) "" + (format "-d \x27%s\x27" entity))) + (curl-command (format "curl %s %s %s %s" header-parsed method-arg url entity-arg))) + (kill-new curl-command) + (message "curl command copied to clipboard."))))) :bind ("C-c C-h" . restclient-call-saved-request) (:map restclient-mode-map ("C-c h" . restclient-save-current)) -- cgit 1.4.1-2-gfad0