Skip to content

response is not formatting json #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
garrettkrohn opened this issue Aug 25, 2024 · 6 comments
Closed

response is not formatting json #417

garrettkrohn opened this issue Aug 25, 2024 · 6 comments

Comments

@garrettkrohn
Copy link

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
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

boltlessengineer commented Aug 31, 2024

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

See this issue: #414 (comment)

Edit: reference

@boltlessengineer
Copy link
Contributor

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
Copy link

Thanks @boltlessengineer thanks for picking this project up!

@jvrviegas
Copy link

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.

Thanks!! I was struggling trying to set the formatting and this config made it work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants