Skip to content

Commit 88ff794

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
fix: parsing of headers and json body
1 parent 86c7598 commit 88ff794

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lua/rest-nvim/curl/init.lua

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local utils = require("rest-nvim.utils")
22
local curl = require("plenary.curl")
33
local config = require("rest-nvim.config")
4-
local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" })
54

65
local M = {}
76
-- get_or_create_buf checks if there is already a buffer with the rest run results
@@ -43,7 +42,6 @@ end
4342
local function create_callback(method, url)
4443
return function(res)
4544
if res.exit ~= 0 then
46-
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
4745
return
4846
end
4947
local res_bufnr = M.get_or_create_buf()

lua/rest-nvim/request/init.lua

+11-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ end
4242
-- @param bufnr Buffer number, a.k.a id
4343
-- @param start_line Line where body starts
4444
-- @param stop_line Line where body stops
45-
local function get_body(bufnr, start_line, stop_line)
45+
-- @param has_hson True if content-type is set to json
46+
local function get_body(bufnr, start_line, stop_line, has_json)
4647
if start_line >= stop_line then
4748
return
4849
end
@@ -73,7 +74,11 @@ local function get_body(bufnr, start_line, stop_line)
7374

7475
local is_json, json_body = pcall(vim.fn.json_decode, body)
7576
if is_json then
76-
return json_body
77+
if has_json then
78+
return vim.fn.json_encode(json_body)
79+
else
80+
return json_body
81+
end
7782
end
7883

7984
return body
@@ -113,12 +118,10 @@ local function get_headers(bufnr, start_line, end_line)
113118
goto continue
114119
end
115120

116-
local header = utils.split(line_content, ":")
117-
local header_name = header[1]:lower()
118-
table.remove(header, 1)
119-
local header_value = table.concat(header, ":")
121+
local header_name, header_value = line_content:match("^(.-): ?(.*)$")
122+
120123
if not utils.contains_comments(header_name) then
121-
headers[header_name] = utils.replace_vars(header_value)
124+
headers[header_name:lower()] = utils.replace_vars(header_value)
122125
end
123126
::continue::
124127
end
@@ -225,7 +228,7 @@ M.get_current_request = function()
225228

226229
local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line)
227230

228-
local body = get_body(bufnr, body_start, end_line)
231+
local body = get_body(bufnr, body_start, end_line, string.find(headers["content-type"] or "", "application/[^ ]-json"))
229232

230233
if config.get("jump_to_request") then
231234
utils.move_cursor(bufnr, start_line)

0 commit comments

Comments
 (0)