Function: verb--export-to-curl

verb--export-to-curl is a natively compiled function defined in verb.el.

Signature

(verb--export-to-curl RS &optional NO-MESSAGE NO-KILL)

Documentation

Export a request spec RS to curl format.

Add the generated command to the kill ring and return it. For more information about curl see URL https://curl.haxx.se/. If NO-MESSAGE is non-nil, do not display a message on the minibuffer. If NO-KILL is non-nil, do not add the command to the kill ring.

Source Code

;; Defined in /nix/store/8jnfn8w2mxcxbr2crbklcnija9m0zlkq-emacs-packages-deps/share/emacs/site-lisp/elpa/verb-20260818.1921/verb.el
(defun verb--export-to-curl (rs &optional no-message no-kill)
  "Export a request spec RS to curl format.
Add the generated command to the kill ring and return it.  For more
information about curl see URL `https://curl.haxx.se/'.  If NO-MESSAGE
is non-nil, do not display a message on the minibuffer.  If NO-KILL is
non-nil, do not add the command to the kill ring."
  (with-temp-buffer
    (insert "curl '" (verb-request-spec-url-to-string rs) "'")
    (dolist (key-value (oref rs headers))
      (insert " \\\n")
      (insert "-H '" (car key-value) ": " (cdr key-value) "'"))
    (unless (string= (oref rs method) "GET")
      (insert (if (oref rs headers) " \\\n" " ")))
    (pcase (oref rs method)
      ;; GET: no argument needed.
      ((or "PATCH" "PUT" "POST")
       (insert "-X "
               (oref rs method)
               " \\\n--data-raw "
               (if (verb-util--seq-contains-p (oref rs body) ?')
                   ;; use heredoc if body contains single quote
                   (concat "\"$(cat <<'VERB_DATA_EOF'\n"
                           (or (oref rs body) "")
                           "\nVERB_DATA_EOF\n)\"")
                 (concat "'" (or (oref rs body) "") "'"))))
      ("DELETE"
       (insert "-X DELETE"))
      ("OPTIONS"
       (insert "-X OPTIONS -i"))
      ("HEAD"
       (insert "-I"))
      ("TRACE"
       (insert "-X TRACE"))
      ("CONNECT"
       (user-error "%s" "CONNECT method not supported in curl format")))
    (let ((result (verb--buffer-string-no-properties)))
      (unless no-kill
        (kill-new result))
      (unless no-message
        (message "Curl command copied to the kill ring"))
      ;; Return the generated command.
      result)))