Skip to content

Commit

Permalink
fix: parsing of headers and json body
Browse files Browse the repository at this point in the history
  • Loading branch information
udayvir-singh authored and NTBBloodbath committed Aug 9, 2022
1 parent 86c7598 commit 88ff794
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 0 additions & 2 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local utils = require("rest-nvim.utils")
local curl = require("plenary.curl")
local config = require("rest-nvim.config")
local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" })

local M = {}
-- get_or_create_buf checks if there is already a buffer with the rest run results
Expand Down Expand Up @@ -43,7 +42,6 @@ end
local function create_callback(method, url)
return function(res)
if res.exit ~= 0 then
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
return
end
local res_bufnr = M.get_or_create_buf()
Expand Down
19 changes: 11 additions & 8 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ end
-- @param bufnr Buffer number, a.k.a id
-- @param start_line Line where body starts
-- @param stop_line Line where body stops
local function get_body(bufnr, start_line, stop_line)
-- @param has_hson True if content-type is set to json
local function get_body(bufnr, start_line, stop_line, has_json)
if start_line >= stop_line then
return
end
Expand Down Expand Up @@ -73,7 +74,11 @@ local function get_body(bufnr, start_line, stop_line)

local is_json, json_body = pcall(vim.fn.json_decode, body)
if is_json then
return json_body
if has_json then
return vim.fn.json_encode(json_body)
else
return json_body
end
end

return body
Expand Down Expand Up @@ -113,12 +118,10 @@ local function get_headers(bufnr, start_line, end_line)
goto continue
end

local header = utils.split(line_content, ":")
local header_name = header[1]:lower()
table.remove(header, 1)
local header_value = table.concat(header, ":")
local header_name, header_value = line_content:match("^(.-): ?(.*)$")

if not utils.contains_comments(header_name) then
headers[header_name] = utils.replace_vars(header_value)
headers[header_name:lower()] = utils.replace_vars(header_value)
end
::continue::
end
Expand Down Expand Up @@ -225,7 +228,7 @@ M.get_current_request = function()

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

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

if config.get("jump_to_request") then
utils.move_cursor(bufnr, start_line)
Expand Down

0 comments on commit 88ff794

Please sign in to comment.