From 46441b631c398924b84e141940415947d1a6cb0b Mon Sep 17 00:00:00 2001 From: Udayvir Singh <97400310+udayvir-singh@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:15:27 +0530 Subject: [PATCH 1/4] fix: parsing of nested tables in request body --- lua/rest-nvim/config/init.lua | 25 +++++++++---------------- lua/rest-nvim/request/init.lua | 8 ++++++++ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lua/rest-nvim/config/init.lua b/lua/rest-nvim/config/init.lua index 036f7f8a..955a3223 100644 --- a/lua/rest-nvim/config/init.lua +++ b/lua/rest-nvim/config/init.lua @@ -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, }, }, diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 71757d5e..cf26eda5 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -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 From f571c4fb869b4e64aa27cc157b26f29cf9cba2e3 Mon Sep 17 00:00:00 2001 From: Udayvir Singh <97400310+udayvir-singh@users.noreply.github.com> Date: Tue, 30 Aug 2022 09:38:51 +0530 Subject: [PATCH 2/4] Add test post_json_form --- tests/post_json_form.http | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/post_json_form.http diff --git a/tests/post_json_form.http b/tests/post_json_form.http new file mode 100644 index 00000000..dd423cf8 --- /dev/null +++ b/tests/post_json_form.http @@ -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" + +} From 0ba5a97215bb2ab5d6314d44fa3ca0b024782f7c Mon Sep 17 00:00:00 2001 From: Udayvir Singh <97400310+udayvir-singh@users.noreply.github.com> Date: Tue, 30 Aug 2022 09:40:20 +0530 Subject: [PATCH 3/4] Fix typo in post_json_form --- tests/post_json_form.http | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/post_json_form.http b/tests/post_json_form.http index dd423cf8..b63e6f3a 100644 --- a/tests/post_json_form.http +++ b/tests/post_json_form.http @@ -10,5 +10,5 @@ POST http://localhost:8080 "array": [1, 2, 3], "json": { "key": "value" - + } } From 48a7c8564b8ee3e0eaafa094d911699d92a89a09 Mon Sep 17 00:00:00 2001 From: Udayvir Singh <97400310+udayvir-singh@users.noreply.github.com> Date: Tue, 13 Sep 2022 16:45:46 +0530 Subject: [PATCH 4/4] fix: parsing of curl arguments --- lua/rest-nvim/request/init.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index cf26eda5..20448fd1 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -112,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 @@ -146,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