pashky / restclient.el

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

Question: json response without request info in comments to ease parsing #141

Open torgeir opened 8 years ago

torgeir commented 8 years ago

Is there a way to perform requests resulting in json responses without including request/response info in comments in the results buffer?

That is, a way to not show responses like this:

{
  "tagline": "You Know, for Search",
  "version": {
    "lucene_version": "4.10.4",
    "build_snapshot": false,
    "build_timestamp": "2015-07-29T09:54:16Z",
    "build_hash": "b88f43fc40b0bcd7f173a1f9ee2e97816de80b19",
    "number": "1.7.1"
  },
  "cluster_name": "es",
  "name": "Bill Foster",
  "status": 200
}
// GET http://127.0.0.1:9200/
// HTTP/1.1 200 OK
// Content-Type: application/json; charset=UTF-8
// Content-Length: 333
// Request duration: 0.023388s

But rather like this:

{
  "tagline": "You Know, for Search",
  "version": {
    "lucene_version": "4.10.4",
    "build_snapshot": false,
    "build_timestamp": "2015-07-29T09:54:16Z",
    "build_hash": "b88f43fc40b0bcd7f173a1f9ee2e97816de80b19",
    "number": "1.7.1"
  },
  "cluster_name": "es",
  "name": "Bill Foster",
  "status": 200
}

Specifically I'm in emacs org mode with babel and ob-restclient, wanting to parse the resulting json for further use within other code blocks.

pashky commented 8 years ago

It feels like this functionality really belongs to ob-restclient and should be implemented through some sort of header flag like :noheaders. So you'd better ask @alf :)

There's C-c C-r command in actual restclient major mode that will leave headers on top as they are in response, but it won't help you.

alf commented 8 years ago

Feel free to open an issue. It's a good feature request and I agree with pashky that it belongs in ob-restclient.

pashky commented 8 years ago

@alf wow, that was quick :) in fact, i looked at my code around so called "prettifying" and felt shame. it must be changed to make it work for ob-restclient. i'll try to cobble together something and perhaps send a pr to you, if i have some spare time this week.

jordonbiondo commented 7 years ago

For others wanting this behavior, here is a snippet from my config that does just this, but only when the status code is 200:

(add-hook 'restclient-response-loaded-hook
              (defun jordon-restclient-delete-headers-when-ok ()
                (when (equal major-mode 'js-mode)
                  (save-excursion
                    (goto-char (point-max))
                    (when (and (search-backward "200 OK" nil t)
                               (search-backward "}" nil t))
                      (forward-char 1)
                      (delete-region (point) (point-max))
                      (json-mode))))))
alf commented 7 years ago

Nice.