rest-nvim / rest.nvim

A fast Neovim http client written in Lua
GNU General Public License v3.0
1.49k stars 137 forks source link

response is not formatting json #417

Closed garrettkrohn closed 2 weeks ago

garrettkrohn commented 2 weeks ago

First off, thank you so much for reviving this project, very excited for it!

I followed the installation instructions, trying both luarocks and lazyvim, but the json responses won't format.

Screenshot 2024-08-24 at 8 56 47 PM

Here are the logs:

DEBUG | 2024-08-24 20:52:21 | ...l/share/nvim/lazy/rest.nvim/lua/rest-nvim/cookie_jar.lua:212 | loading cookies for request:https://swapi.dev/api/people/
DEBUG | 2024-08-24 20:52:21 | ...l/share/nvim/lazy/rest.nvim/lua/rest-nvim/cookie_jar.lua:197 | cookie JSESSIONID with domain .localhost:4444 and path /api NOT matched to url: https://swapi.dev/api/people/
DEBUG | 2024-08-24 20:52:21 | ...ocal/share/nvim/lazy/rest.nvim/lua/rest-nvim/request.lua:46 | running request:starwars#1
INFO | 2024-08-24 20:52:21 | ...re/nvim/lazy/rest.nvim/lua/rest-nvim/client/curl/cli.lua:33 | { "curl", "-sL", "-v", "https://swapi.dev/api/people/", "-X", "GET", "-H", "User-Agent: rest.nvim v3.0.0", "-w", "%{stderr}? time_total:%{time_total}\n? size_download:%{size_download}\n" }
ERROR | 2024-08-24 20:52:22 | ...re/nvim/lazy/rest.nvim/lua/rest-nvim/client/curl/cli.lua:81 | Error while parsing verbose curl output:

INFO | 2024-08-24 20:52:22 | ...ocal/share/nvim/lazy/rest.nvim/lua/rest-nvim/request.lua:73 | request success
INFO | 2024-08-24 20:52:22 | ...ocal/share/nvim/lazy/rest.nvim/lua/rest-nvim/request.lua:79 | handler done
DEBUG | 2024-08-24 20:52:22 | ...al/share/nvim/lazy/rest.nvim/lua/rest-nvim/ui/result.lua:69 | {
  code = 200,
  text = "",
  version = "HTTP/2"
}
DEBUG | 2024-08-24 20:52:22 | ...al/share/nvim/lazy/rest.nvim/lua/rest-nvim/ui/result.lua:111 | {
  allow = { "GET, HEAD, OPTIONS" },
  ["content-type"] = { "application/json" },
  date = { "Sun, 25 Aug 2024 01:52:22 GMT" },
  etag = { '"b493126da505af6fec015ec116fec193"' },
  server = { "nginx/1.16.1" },
  ["strict-transport-security"] = { "max-age=15768000" },
  vary = { "Accept, Cookie" },
  ["x-frame-options"] = { "SAMEORIGIN" }
}
DEBUG | 2024-08-24 20:52:22 | ...al/share/nvim/lazy/rest.nvim/lua/rest-nvim/ui/result.lua:116 | { { "allow", { "GET, HEAD, OPTIONS" },
    <metatable> = <1>{}
  }, { "content-type", { "application/json" },
    <metatable> = <table 1>
  }, { "date", { "Sun, 25 Aug 2024 01:52:22 GMT" },
    <metatable> = <table 1>
  }, { "etag", { '"b493126da505af6fec015ec116fec193"' },
    <metatable> = <table 1>
  }, { "server", { "nginx/1.16.1" },
    <metatable> = <table 1>
  }, { "strict-transport-security", { "max-age=15768000" },
    <metatable> = <table 1>
  }, { "vary", { "Accept, Cookie" },
    <metatable> = <table 1>
  }, { "x-frame-options", { "SAMEORIGIN" },
    <metatable> = <table 1>
  } }

I've done as much digging as I can and it seems to be this line: Error while parsing verbose curl output. The odd thing is it prints a blank line, so maybe a blank line is breaking it? Trying to learn more about neovim plugins, but definitely out of my element. Thank you!

boltlessengineer commented 2 weeks ago

The error log isn’t related to formatting. Do you have formatexpr or formatprg set for json filetype? That might be the issue here. rest.nvim uses native gq command for formatting. If you don’t set formatexpr or formatprg option for json filetype, gq command won’t be able to format response body properly. Try open json file and type gggqG. If that doesn’t work that means you don’t have any formatter attached to json filetype. Comment I wrote in other issue might help.

I should add this check to checkhealth command.

chardskarth commented 2 weeks ago

I have formatexpr already set but not formatprg. Formatting was fixed for me when I added the following in my config:

vim.api.nvim_create_autocmd("FileType",  {
      pattern = { "json" },
      callback = function()
        vim.api.nvim_set_option_value("formatprg", "jq", { scope = 'local' })
      end,
})

But ofcourse I already have jq installed.

boltlessengineer commented 2 weeks ago

@chardskarth what formatexpr did you set? v:lua.vim.lsp.formatexpr() won’t work.

See this issue: https://github.com/rest-nvim/rest.nvim/issues/414#issuecomment-2308910953

Edit: reference

boltlessengineer commented 2 weeks ago

Closing the issue as this seems to be mainly a vim.lsp.formatexpr() issue. If formatting still doesn’t work, please create a new issue with reproducible steps.

chardskarth commented 1 week ago

Thanks @boltlessengineer thanks for picking this project up!