Skip to content

Commit

Permalink
feat: add support for passing flags to curl
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 d902996 commit 3c46649
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ local function create_callback(method, url)
-- Check if the content-type is "application/json" so we can format the JSON
-- output later
for _, header in ipairs(res.headers) do
if header:find("application") and header:find("json") then
if header:find("application/json") then
json_body = true
break
end
Expand Down Expand Up @@ -92,7 +92,7 @@ local function create_callback(method, url)
--- Add the curl command results into the created buffer
if json_body then
-- format JSON body
res.body = vim.fn.system("jq", res.body)
res.body = vim.fn.system("jq", res.body):gsub("\n$", "")
end
local lines = utils.split(res.body, "\n")
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
Expand Down
2 changes: 1 addition & 1 deletion lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rest.run = function(verbose)
method = result.method:lower(),
url = result.url,
headers = result.headers,
raw = config.get("skip_ssl_verification") and { "-k" } or nil,
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" }) or result.raw,
body = result.body,
dry_run = verbose or false,
bufnr = result.bufnr,
Expand Down
41 changes: 37 additions & 4 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
-- @param end_line Line where the request ends
local function get_headers(bufnr, start_line, end_line)
local headers = {}
local body_start = end_line
local headers_end = end_line

-- Iterate over all buffer lines starting after the request line
for line_number = start_line + 1, end_line do
Expand All @@ -105,7 +105,7 @@ local function get_headers(bufnr, start_line, end_line)
-- 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
if is_request_line(line_content) or line_content == "" then
body_start = line_number
headers_end = line_number
break
end
if not line_content:find(":") then
Expand All @@ -123,7 +123,32 @@ local function get_headers(bufnr, start_line, end_line)
::continue::
end

return headers, body_start
return headers, headers_end
end

-- get_curl_args finds command line flags and returns a lua table with them
-- @param bufnr Buffer number, a.k.a id
-- @param headers_end Line where the headers end
-- @param end_line Line where the request ends
local function get_curl_args(bufnr, headers_end, end_line)
local curl_args = {}
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]

if line_content:find("^ *%-%-?[a-zA-Z%-]+") then
table.insert(curl_args, line_content)
elseif not line_content:find("^ *$") then
if line_number ~= end_line then
body_start=line_number - 1
end
break
end
end

return curl_args, body_start
end

-- start_request will find the request line (e.g. POST http://localhost:8081/foo)
Expand Down Expand Up @@ -159,6 +184,11 @@ local function end_request(bufnr)
if next == 0 or (oldlinenumber == last_line) then
return last_line
else
-- skip comment lines above requests
while vim.fn.getline(next - 1):find("^ *#") do
next = next - 1
end

return next - 1
end
end
Expand Down Expand Up @@ -191,7 +221,9 @@ M.get_current_request = function()

local parsed_url = parse_url(vim.fn.getline(start_line))

local headers, body_start = get_headers(bufnr, start_line, end_line)
local headers, headers_end = get_headers(bufnr, start_line, end_line)

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

local body = get_body(bufnr, body_start, end_line)

Expand All @@ -205,6 +237,7 @@ M.get_current_request = function()
method = parsed_url.method,
url = parsed_url.url,
headers = headers,
raw = curl_args,
body = body,
bufnr = bufnr,
start_line = start_line,
Expand Down

0 comments on commit 3c46649

Please sign in to comment.