Skip to content

Commit 3c46649

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
feat: add support for passing flags to curl
1 parent d902996 commit 3c46649

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

lua/rest-nvim/curl/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ local function create_callback(method, url)
5252
-- Check if the content-type is "application/json" so we can format the JSON
5353
-- output later
5454
for _, header in ipairs(res.headers) do
55-
if header:find("application") and header:find("json") then
55+
if header:find("application/json") then
5656
json_body = true
5757
break
5858
end
@@ -92,7 +92,7 @@ local function create_callback(method, url)
9292
--- Add the curl command results into the created buffer
9393
if json_body then
9494
-- format JSON body
95-
res.body = vim.fn.system("jq", res.body)
95+
res.body = vim.fn.system("jq", res.body):gsub("\n$", "")
9696
end
9797
local lines = utils.split(res.body, "\n")
9898
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1

lua/rest-nvim/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ rest.run = function(verbose)
2323
method = result.method:lower(),
2424
url = result.url,
2525
headers = result.headers,
26-
raw = config.get("skip_ssl_verification") and { "-k" } or nil,
26+
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" }) or result.raw,
2727
body = result.body,
2828
dry_run = verbose or false,
2929
bufnr = result.bufnr,

lua/rest-nvim/request/init.lua

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595
-- @param end_line Line where the request ends
9696
local function get_headers(bufnr, start_line, end_line)
9797
local headers = {}
98-
local body_start = end_line
98+
local headers_end = end_line
9999

100100
-- Iterate over all buffer lines starting after the request line
101101
for line_number = start_line + 1, end_line do
@@ -105,7 +105,7 @@ local function get_headers(bufnr, start_line, end_line)
105105
-- message header and message body are seperated by CRLF (see RFC 2616)
106106
-- for our purpose also the next request line will stop the header search
107107
if is_request_line(line_content) or line_content == "" then
108-
body_start = line_number
108+
headers_end = line_number
109109
break
110110
end
111111
if not line_content:find(":") then
@@ -123,7 +123,32 @@ local function get_headers(bufnr, start_line, end_line)
123123
::continue::
124124
end
125125

126-
return headers, body_start
126+
return headers, headers_end
127+
end
128+
129+
-- get_curl_args finds command line flags and returns a lua table with them
130+
-- @param bufnr Buffer number, a.k.a id
131+
-- @param headers_end Line where the headers end
132+
-- @param end_line Line where the request ends
133+
local function get_curl_args(bufnr, headers_end, end_line)
134+
local curl_args = {}
135+
local body_start = end_line
136+
137+
for line_number = headers_end, end_line do
138+
local line = vim.fn.getbufline(bufnr, line_number)
139+
local line_content = line[1]
140+
141+
if line_content:find("^ *%-%-?[a-zA-Z%-]+") then
142+
table.insert(curl_args, line_content)
143+
elseif not line_content:find("^ *$") then
144+
if line_number ~= end_line then
145+
body_start=line_number - 1
146+
end
147+
break
148+
end
149+
end
150+
151+
return curl_args, body_start
127152
end
128153

129154
-- start_request will find the request line (e.g. POST http://localhost:8081/foo)
@@ -159,6 +184,11 @@ local function end_request(bufnr)
159184
if next == 0 or (oldlinenumber == last_line) then
160185
return last_line
161186
else
187+
-- skip comment lines above requests
188+
while vim.fn.getline(next - 1):find("^ *#") do
189+
next = next - 1
190+
end
191+
162192
return next - 1
163193
end
164194
end
@@ -191,7 +221,9 @@ M.get_current_request = function()
191221

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

194-
local headers, body_start = get_headers(bufnr, start_line, end_line)
224+
local headers, headers_end = get_headers(bufnr, start_line, end_line)
225+
226+
local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line)
195227

196228
local body = get_body(bufnr, body_start, end_line)
197229

@@ -205,6 +237,7 @@ M.get_current_request = function()
205237
method = parsed_url.method,
206238
url = parsed_url.url,
207239
headers = headers,
240+
raw = curl_args,
208241
body = body,
209242
bufnr = bufnr,
210243
start_line = start_line,

0 commit comments

Comments
 (0)