rest.nvim插件问题:JSON响应无法正常格式化
rest.nvim插件JSON响应无法用jq格式化的问题与解决
问题描述
在Neovim中使用rest.nvim插件发送HTTP请求时,已配置jq作为JSON响应格式化工具,但实际返回的是未格式化的原始JSON字符串,未达到预期的格式化效果。
已执行的排查步骤
- 确认系统已安装jq工具
- 验证rest.nvim插件已正确安装并完成配置
- 查阅插件官方文档及相关论坛,未找到同类问题的解决方案
示例请求文件(testing.http)
GET https://random-data-api.com/api/address/random_address Content-Type: application/json
实际响应内容
GET https://random-data-api.com/api/address/random_address #+END #+RESPONSE {"id":3318,"uid":"09951d24-0ba7-46e7-90c9-c75a1d1fa014","city":"Buckridgetown","street_name":"Eusebio Viaduct","street_address":"11676 Reatha Road","secondary_address":"Suite 204","building_number":"3557","mail_box":"PO Box 7129","community":"Willow Village","zip_code":"37775","zip":"42017-6516","postcode":"25460","time_zone":"America/St_Johns","street_suffix":"Park","city_suffix":"stad","city_prefix":"North","state":"Rhode Island","state_abbr":"WI","country":"United States Minor Outlying Islands","country_code":"PG","latitude":-74.28963865772323,"longitude":67.39583760610813,"full_address":"6652 Sanford Vista, South Trudichester, FL 91950-9038"} #+END
当前插件配置
return { "rest-nvim/rest.nvim", requires = { "nvim-lua/plenary.nvim" }, config = function() require("rest-nvim").setup({ -- Open request results in a horizontal split result_split_horizontal = false, -- Keep the http file buffer above|left when split horizontal|vertical result_split_in_place = false, -- Skip SSL verification, useful for unknown certificates skip_ssl_verification = false, -- Encode URL before making request encode_url = true, -- Highlight request on run highlight = { enabled = true, timeout = 150, }, result = { -- Toggle showing URL, HTTP info, headers at top of result window show_url = true, -- Show the generated curl command in case you want to launch -- the same request via the terminal (can be verbose) show_curl_command = false, show_http_info = true, show_headers = false, -- Executables or functions for formatting response body [optional] -- Set them to false if you want to disable them formatters = { json = "jq", html = function(body) return vim.fn.system({ "tidy", "-i", "-q", "-" }, body) end, }, }, -- Jump to request line on run jump_to_request = false, env_file = ".env", custom_dynamic_variables = {}, yank_dry_run = true, }) local opts = { noremap = true, silent = true } vim.api.nvim_set_keymap("n", "<leader>rr", "<Plug>RestNvim", opts) vim.api.nvim_set_keymap("n", "<leader>rp", "<Plug>RestNvimPreview", opts) vim.api.nvim_set_keymap("n", "<leader>rl", "<Plug>RestNvimLast", opts) end, }
解决方案
该问题源于rest.nvim插件的破坏性更新,临时修复方案是在插件配置中指定更新前的特定commit:
return { { "rest-nvim/rest.nvim", requires = { "nvim-lua/plenary.nvim" }, -- 最新commit破坏了格式化功能 commit = "8b62563", opts = { result_split_in_place = true, }, }, }
内容的提问来源于stack exchange,提问作者ale
相关产品推荐
相关产品推荐

