
纯文本有很多好处,比如可以用 Git 管理,方便备份和追溯,方便多机同步和分享。
之前使用 VS Code 的插件 Rest Client ,从浏览器后台复制请求数据过来就可以用,很方便。但是存在一些用起来不舒服的地方,比如:
这些在对纯文本作效率更高的 Vim 里都可以解决,但是之前尝试过的一些 Vim 的 RESTful 客户端插件还不成熟,最近发现 rest.nvim 基本可以满足日常使用需要了。
rest.nvim 是个 Neovim 的插件,和 Rest Client 支持的格式差异较小,迁移很方便。这样我就可以用 Vim 的折叠特性对接口分组管理,用插件 Leaderf 的模糊查找特性快速定位接口,当然,对纯文本的任何编辑和操作在 Vim 里就是浑然天成、行云流水的。
它的大致用法如下:
在工作目录下创建环境变量文件 .env :
base_url=http://myapp.dev header_accept_json=application/json, application/problem+json, text/plain, */* header_content_type_json=application/json;charset= header_cookie_debug_session=XDEBUG_SESSION=1;app_session=1ObUjvLvEYjVhJ8tbzn5BorN7TViNtI1S625140e [email protected] user_password=password 在工作目录下创建请求文件(例如 myapp.http ):
### Login POST {{base_url}}/v1/sessions Accept: {{header_accept_json}} Content-Type: {{header_content_type_json}} Cookie: {{header_cookie_debug_session}} {"email": "{{user_email}}", "password": "{{user_password}}"} ### Get an article GET {{base_url}}/v1/articles/DiJeb7IQHo8FOFkXulieyA Accept: {{header_accept_json}} Cookie: {{header_cookie_debug_session}} ### Create an article POST {{base_url}}/v1/articles Accept: {{header_accept_json}} Cookie: {{header_cookie_debug_session}} Content-Type: {{header_content_type_json}} { "title": "Hello world", "Content": "This is a dummy post." } 然后就可以用插件提供的命令触发请求了。
不过这个插件目前还不能保持会话,所以每次请求完登录接口都要手动把会话信息复制到环境变量中,比较麻烦。这里我用 Vim 自身的机制来解决:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " " Auto-save the session cookie to .env for rest.nvim " """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Set the name of the variable in the .env file which stores the HTTP request " header for cookie: " e.g. header_cookie_debug_session=XDEBUG_SESSION=1;app_session=I6VFFDmpeZ2uqMQhRqxaBAoRv6LypXS6phkFYbxg let g:env_var_http_request_header_cookie = 'header_cookie_debug_session' " Set the key contained in the Set-Cookie HTTP response header which holds the " session cookie let g:http_response_cookie_session = 'app_session' autocmd BufEnter * if &filetype == 'httpResult' | call <SID>save_cookie() | endif function s:save_cookie() abort let l:cookie = matchstr(getline(search('Set-Cookie:')), '\(' . g:http_response_cookie_session . '=\)\@<=\(.\{-}\)\(;\)\@=') if l:cookie == '' echoerr 'Cannot find a valid cookie.' return endif let l:bak_file_ext_part = '' if IsPlatform('mac') let l:bak_file_ext_part = '""' endif call system('sed -i ' . l:bak_file_ext_part . ' "s/\(' . g:env_var_http_request_header_cookie . '=.*' . g:http_response_cookie_session . '=\)[0-9A-Za-z]*/\1' . l:cookie . '/" .env') endfunction function! IsPlatform(mixed)"{{{ if type(a:mixed) == 1 let plist = [a:mixed] elseif type(a:mixed) == 3 let plist = a:mixed else return 0 endif if has('win16') || has('win32') || has('win64') || has('winnt') return index(plist, 'win')>=0 elseif has('mac') return index(plist, 'mac')>=0 else return index(plist, 'unix')>=0 endif return 0 endfunction"}}} 这样,每次请求完登录接口就会自动把 cookie 写入 .env 文件中了。
原文: http://0x3f.org/posts/send-restful-requests-with-neovim/
1 975779964 2022-09-24 09:58:14 +08:00 谢谢楼主,非常好用 ,我想问下,请求的结果 的折叠 我这里不起作用呢,我用的是 treesitter 的折叠 |
2 donieleigh OP @975779964 我是这么实现的: ```vim autocmd BufEnter * if &filetype == 'httpResult' | setl fdm=indent | setl fdl=1 | endif ``` |
3 Kaiv2 2022-10-27 21:02:47 +08:00 有个问题 rest.nvim 会解析 body json 发送时会打乱顺序,使用签名的场景下午饭使用。 感觉是 lua 解析后没有保证顺序。 |
| /td> | 4 donieleigh OP @Kaiv2 #3 rest.nvim 是通过 curl 发送请求和接收响应、通过 jq 格式化 json 的,应该不是通过 lua 解析的。你可以排查下是哪个环节出的问题,应该也能找到解决办法。 |