Skip to content

fix: formatter for html and special casing of content-type #170

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

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use {
show_http_info = true,
show_headers = true,
-- executables or functions for formatting response body [optional]
-- set them to nil if you want to disable them
-- set them to false if you want to disable them
formatters = {
json = "jq",
html = function(body)
Expand Down
3 changes: 3 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ local config = {
formatters = {
json = "jq",
html = function(body)
if vim.fn.executable("tidy") == 0 then
return body
end
-- stylua: ignore
return vim.fn.system({
"tidy", "-i", "-q",
Expand Down
13 changes: 11 additions & 2 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ local function get_body(bufnr, start_line, stop_line, has_json)
end
end

local is_json, json_body = pcall(vim.fn.json_decode, body)
local is_json, json_body = pcall(vim.json.decode, body)

if is_json then
if has_json then
Expand Down Expand Up @@ -280,11 +280,20 @@ M.buf_get_request = function(bufnr, curpos)
headers["host"] = nil
end

local content_type = ""

for key, val in pairs(headers) do
if string.lower(key) == "content-type" then
content_type = val
break
end
end

local body = get_body(
bufnr,
body_start,
end_line,
string.find(headers["content-type"] or "", "application/[^ ]-json")
content_type:find("application/[^ ]*json")
)

if config.get("jump_to_request") then
Expand Down