前言

在日常开发中,经常需要调试服务端返回的数据,我们常用的有 PostmanPostWoman 等等。

但是我习惯了在 Emacs 中做完这一切,不想去安装其他软件,于是在 Emacs 中也找到了 restclient 这个插件,它能满足我的需求。

所以今天要来分享一下 restclient 的使用。

安装插件

先来说下插件的安装,需要在你的 Emacs 配置文件中加入如下配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
(use-package restclient
  :mode ("\\.http\\'" . restclient-mode)
  :config
  (with-eval-after-load 'company
    (use-package company-restclient
      :defines company-backends
      :init (add-to-list 'company-backends 'company-restclient))))

(use-package ob-restclient
  :init (cl-pushnew '(restclient . t) load-language-list))

我们需要两个插件,一个是 restclient 这个是用来发送请求的,另一个是 ob-restclient 这一个是让你在 org-mode 中也能使用 restclient 的功能。

company-restclient 是用来提示的,在你输入一部分之后会帮你补全剩下的关键字。

本文都是在 org-mode 中使用,所以你只要在 org-mode 中的 org-babel 中使用就行,如下所示

1
2
3
#+begin_src restclient
这里写网络请求
#+end_src

GET

插件装好之后,我们来发起一个 GET 请求看看

1
2
3
#+begin_src restclient
GET https://httpbin.org/get
#+end_src

按下快捷键 C-c C-c 执行,得到结果如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Charset": "utf-8;q=1, gb2312;q=0.5, iso-8859-1;q=0.5, big5;q=0.5, iso-2022-jp;q=0.5, shift_jis;q=0.5, euc-tw;q=0.5, euc-jp;q=0.5, euc-jis-2004;q=0.5, euc-kr;q=0.5, us-ascii;q=0.5, utf-7;q=0.5, hz-gb-2312;q=0.5, big5-hkscs;q=0.5, gbk;q=0.5, gb18030;q=0.5, iso-8859-5;q=0.5, koi8-r;q=0.5, koi8-u;q=0.5, cp866;q=0.5, koi8-t;q=0.5, windows-1251;q=0.5, cp855;q=0.5, iso-8859-2;q=0.5, iso-8859-3;q=0.5, iso-8859-4;q=0.5, iso-8859-9;q=0.5, iso-8859-10;q=0.5, iso-8859-13;q=0.5, iso-8859-14;q=0.5, iso-8859-15;q=0.5, windows-1250;q=0.5, windows-1252;q=0.5, windows-1254;q=0.5, windows-1257;q=0.5, cp775;q=0.5, cp850;q=0.5, cp852;q=0.5, cp857;q=0.5, cp858;q=0.5, cp860;q=0.5, cp861;q=0.5, cp863;q=0.5, cp865;q=0.5, cp437;q=0.5, macintosh;q=0.5, next;q=0.5, hp-roman8;q=0.5, adobe-standard-encoding;q=0.5, iso-8859-16;q=0.5, iso-8859-7;q=0.5, windows-1253;q=0.5, cp737;q=0.5, cp851;q=0.5, cp869;q=0.5, iso-8859-8;q=0.5, windows-1255;q=0.5, cp862;q=0.5, iso-2022-jp-2004;q=0.5, cp874;q=0.5, iso-8859-11;q=0.5, viscii;q=0.5, windows-1258;q=0.5, iso-8859-6;q=0.5, windows-1256;q=0.5, iso-2022-cn;q=0.5, iso-2022-cn-ext;q=0.5, iso-2022-jp-2;q=0.5, iso-2022-kr;q=0.5, utf-16le;q=0.5, utf-16be;q=0.5, utf-16;q=0.5, x-ctext;q=0.5",
    "Content-Length": "0",
    "Extension": "Security/Digest Security/SSL",
    "Host": "httpbin.org",
    "Mime-Version": "1.0",
    "X-Amzn-Trace-Id": "Root=1-6270a5ab-51072d1c2ec2bdaa3ac54621"
  },
  "origin": "xxx.xxx.xxx.xxx",
  "url": "https://httpbin.org/get"
}

// GET https://httpbin.org/get
// HTTP/1.1 200 OK
// Date: Tue, 03 May 2022 03:46:51 GMT
// Content-Type: application/json
// Content-Length: 1557
// Connection: keep-alive
// Server: gunicorn/19.9.0
// Access-Control-Allow-Origin: *
// Access-Control-Allow-Credentials: true
// Request duration: 0.863860s

设置Header

设置Header 跟在请求行后面就行,也就是 http 规范。

这里以 Accept-EncodingUser-Agent 为例,如下所示

1
2
3
4
5
#+begin_src restclient
GET https://httpbin.org/get
Accept-Encoding: compress, gzip
User-Agent: Emacs Restclient
#+end_src

请求结果如下,可以看到返回的 User-Agent 就是我们设置的 Emacs Restclient

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "0",
    "Extension": "Security/Digest Security/SSL",
    "Host": "httpbin.org",
    "If-Modified-Since": "Tue, 03 May 2022 03:46:52 GMT",
    "Mime-Version": "1.0",
    "User-Agent": "Emacs Restclient",
    "X-Amzn-Trace-Id": "Root=1-6270a613-0ce11c2253b6e145184a33f3"
  },
  "origin": "xxx.xxx.xxx.xxx",
  "url": "https://httpbin.org/get"
}

// GET https://httpbin.org/get
// HTTP/1.1 200 OK
// Date: Tue, 03 May 2022 03:48:35 GMT
// Content-Type: application/json
// Content-Length: 459
// Connection: keep-alive
// Server: gunicorn/19.9.0
// Access-Control-Allow-Origin: *
// Access-Control-Allow-Credentials: true
// Request duration: 2.270525s

POST

接下来我们来看 Post 方法怎么使用,按照 http 协议规范, Post 的内容要放在 header 后面,使用一个换行符区分,如下所示

1
2
3
4
5
6
7
8
9
#+begin_src restclient
POST https://httpbin.org/post
Content-Type: application/json

{
    "username": "test",
    "pwd": "test"
}
#+end_src

结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
  "args": {},
  "data": "{\n    \"username\": \"test\",\n    \"pwd\": \"test\"\n}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "45",
    "Content-Type": "application/json",
    "Extension": "Security/Digest Security/SSL",
    "Host": "httpbin.org",
    "Mime-Version": "1.0",
    "X-Amzn-Trace-Id": "Root=1-6270a68d-31ac0d573e19fbb258e19d23"
  },
  "json": {
    "pwd": "test",
    "username": "test"
  },
  "origin": "xxx.xxx.xxx.xxx",
  "url": "https://httpbin.org/post"
}

// POST https://httpbin.org/post
// HTTP/1.1 200 OK
// Date: Tue, 03 May 2022 03:50:37 GMT
// Content-Type: application/json
// Content-Length: 567
// Connection: keep-alive
// Server: gunicorn/19.9.0
// Access-Control-Allow-Origin: *
// Access-Control-Allow-Credentials: true
// Request duration: 1.104782s

总结

有关 restclient 的今天就说这么多,如果你感兴趣,官网还有一些更高级的用法,可以前往查看。官网地址在参考中。

1
GET https://httpbin.org

参考