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..20448fd1 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 @@ -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 @@ -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 diff --git a/tests/post_json_form.http b/tests/post_json_form.http new file mode 100644 index 00000000..b63e6f3a --- /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" + } +}