Skip to content

Commit 3811092

Browse files
authored
Merge pull request #138 from udayvir-singh/main
fix parsing of nested tables and curl arguments
2 parents 2e368cd + 48a7c85 commit 3811092

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

lua/rest-nvim/config/init.lua

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,15 @@ local config = {
1616
formatters = {
1717
json = "jq",
1818
html = function(body)
19-
return vim.fn
20-
.system({
21-
"tidy",
22-
"-i",
23-
"-q",
24-
"--tidy-mark",
25-
"no",
26-
"--show-body-only",
27-
"auto",
28-
"--show-errors",
29-
"0",
30-
"--show-warnings",
31-
"0",
32-
"-",
33-
}, body)
34-
:gsub("\n$", "")
19+
-- stylua: ignore
20+
return vim.fn.system({
21+
"tidy", "-i", "-q",
22+
"--tidy-mark", "no",
23+
"--show-body-only", "auto",
24+
"--show-errors", "0",
25+
"--show-warnings", "0",
26+
"-",
27+
}, body):gsub("\n$", "")
3528
end,
3629
},
3730
},

lua/rest-nvim/request/init.lua

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,18 @@ local function get_body(bufnr, start_line, stop_line, has_json)
6868
end
6969

7070
local is_json, json_body = pcall(vim.fn.json_decode, body)
71+
7172
if is_json then
7273
if has_json then
74+
-- convert entire json body to string.
7375
return vim.fn.json_encode(json_body)
7476
else
77+
-- convert nested tables to string.
78+
for key, val in pairs(json_body) do
79+
if type(val) == "table" then
80+
json_body[key] = vim.fn.json_encode(val)
81+
end
82+
end
7583
return json_body
7684
end
7785
end
@@ -100,8 +108,7 @@ local function get_headers(bufnr, start_line, end_line)
100108

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

106113
-- message header and message body are seperated by CRLF (see RFC 2616)
107114
-- for our purpose also the next request line will stop the header search
@@ -134,11 +141,24 @@ local function get_curl_args(bufnr, headers_end, end_line)
134141
local body_start = end_line
135142

136143
for line_number = headers_end, end_line do
137-
local line = vim.fn.getbufline(bufnr, line_number)
138-
local line_content = line[1]
144+
local line_content = vim.fn.getbufline(bufnr, line_number)[1]
139145

140146
if line_content:find("^ *%-%-?[a-zA-Z%-]+") then
141-
table.insert(curl_args, line_content)
147+
local lc = vim.split(line_content, " ")
148+
local x = ""
149+
150+
for i, y in ipairs(lc) do
151+
x = x .. y
152+
153+
if #y:match("\\*$") % 2 == 1 and i ~= #lc then
154+
-- insert space if there is an slash at end
155+
x = x .. " "
156+
else
157+
-- insert 'x' into curl_args and reset it
158+
table.insert(curl_args, x)
159+
x = ""
160+
end
161+
end
142162
elseif not line_content:find("^ *$") then
143163
if line_number ~= end_line then
144164
body_start = line_number - 1

tests/post_json_form.http

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Run:
2+
# $ python -m http.server 8080
3+
4+
# it should return status code 501
5+
POST http://localhost:8080
6+
7+
{
8+
"string": "foo",
9+
"number": 100,
10+
"array": [1, 2, 3],
11+
"json": {
12+
"key": "value"
13+
}
14+
}

0 commit comments

Comments
 (0)