Skip to content

fix parsing of nested tables and curl arguments #138

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 4 commits into from
Nov 25, 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
25 changes: 9 additions & 16 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ local config = {
formatters = {
json = "jq",
html = function(body)
return vim.fn
.system({
"tidy",
"-i",
"-q",
"--tidy-mark",
"no",
"--show-body-only",
"auto",
"--show-errors",
"0",
"--show-warnings",
"0",
"-",
}, body)
:gsub("\n$", "")
-- stylua: ignore
return vim.fn.system({
"tidy", "-i", "-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
end,
},
},
Expand Down
30 changes: 25 additions & 5 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ local function get_body(bufnr, start_line, stop_line, has_json)
end

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

if is_json then
if has_json then
-- convert entire json body to string.
return vim.fn.json_encode(json_body)
else
-- convert nested tables to string.
for key, val in pairs(json_body) do
if type(val) == "table" then
json_body[key] = vim.fn.json_encode(val)
end
end
return json_body
end
end
Expand Down Expand Up @@ -104,8 +112,7 @@ local function get_headers(bufnr, start_line, end_line)

-- Iterate over all buffer lines starting after the request line
for line_number = start_line + 1, end_line do
local line = vim.fn.getbufline(bufnr, line_number)
local line_content = line[1]
local line_content = vim.fn.getbufline(bufnr, line_number)[1]

-- message header and message body are seperated by CRLF (see RFC 2616)
-- for our purpose also the next request line will stop the header search
Expand Down Expand Up @@ -138,11 +145,24 @@ local function get_curl_args(bufnr, headers_end, end_line)
local body_start = end_line

for line_number = headers_end, end_line do
local line = vim.fn.getbufline(bufnr, line_number)
local line_content = line[1]
local line_content = vim.fn.getbufline(bufnr, line_number)[1]

if line_content:find("^ *%-%-?[a-zA-Z%-]+") then
table.insert(curl_args, line_content)
local lc = vim.split(line_content, " ")
local x = ""

for i, y in ipairs(lc) do
x = x .. y

if #y:match("\\*$") % 2 == 1 and i ~= #lc then
-- insert space if there is an slash at end
x = x .. " "
else
-- insert 'x' into curl_args and reset it
table.insert(curl_args, x)
x = ""
end
end
elseif not line_content:find("^ *$") then
if line_number ~= end_line then
body_start = line_number - 1
Expand Down
14 changes: 14 additions & 0 deletions tests/post_json_form.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Run:
# $ python -m http.server 8080

# it should return status code 501
POST http://localhost:8080

{
"string": "foo",
"number": 100,
"array": [1, 2, 3],
"json": {
"key": "value"
}
}