pashky / restclient.el

HTTP REST client tool for emacs
1.99k stars 165 forks source link

Feature request: generate curl command with quotes instead of backslashes #288

Open jjnilton opened 1 year ago

jjnilton commented 1 year ago

Request block:

POST https://example.com
Content-Type: application/json
{
    "order_ref": "12321312",
    "total": 123,
    "items": [
        {
            "item_title": "Payment Test in Digital Wallet",
            "unit_price": 123,
            "quantity": 1
        }
    ],
    "buyer": {
        "name": "Foo bar",
        "personId": "123",
        "phone": "123"
    },
    "callback_url": "https:\/\/example.com\/example\/example\/v1\/example\/example\/example\/123"
}

Current curl command from the block above:

curl -i -H Content-Type\:\ application/json -XPOST https\://example.com -d \{'
'\ \ \ \ \"order_ref\"\:\ \"12321312\"\,'
'\ \ \ \ \"total\"\:\ 123\,'
'\ \ \ \ \"items\"\:\ \['
'\ \ \ \ \ \ \ \ \{'
'\ \ \ \ \ \ \ \ \ \ \ \ \"item_title\"\:\ \"Payment\ Test\ in\ Digital\ Wallet\"\,'
'\ \ \ \ \ \ \ \ \ \ \ \ \"unit_price\"\:\ 123\,'
'\ \ \ \ \ \ \ \ \ \ \ \ \"quantity\"\:\ 1'
'\ \ \ \ \ \ \ \ \}'
'\ \ \ \ \]\,'
'\ \ \ \ \"buyer\"\:\ \{'
'\ \ \ \ \ \ \ \ \"name\"\:\ \"Foo\ bar\"\,\ '
'\ \ \ \ \ \ \ \ \"personId\"\:\ \"123\"\,'
'\ \ \ \ \ \ \ \ \"phone\"\:\ \"123\"'
'\ \ \ \ \}\,'
'\ \ \ \ \"callback_url\"\:\ \"https\:\\/\\/example.com\\/example\\/example\\/v1\\/example\\/example\\/example\\/123\"'
'\}'
'

Suggested curl command output:

curl -i -H "Content-Type: application/json" -XPOST https://example.com -d '{
    "order_ref": "12321312",
    "total": 123,
    "items": [
        {
            "item_title": "Payment Test in Digital Wallet",
            "unit_price": 123,
            "quantity": 1
        }
    ],
    "buyer": {
        "name": "Foo bar",
        "personId": "123",
        "phone": "123"
    },
    "callback_url": "https://example.com/example/example/v1/example/example/example/123"
}'

I'm not sure of the drawback, but the benefit is that it would a bit cleaner, so it would be easier to read/edit.

failable commented 11 months ago

+1

ccarlile commented 10 months ago

I have this in my init.el to do exactly that:

(defun mine-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.")))))
Yevgnen commented 10 months ago

@ccarlile That's cool! Works like a charm.