Skip to content

Commit 248a07c

Browse files
committed
feat: make it possible to not inline external file
rest.nvim always inlines the payload which generates humongous (superlong) curl commands. If you prefix your external payload with `<@` instead of `<`, rest.nvim will pass the file to curl as-is via --data-binary @filename
1 parent 8f7d45a commit 248a07c

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

lua/rest-nvim/curl/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ local function create_callback(curl_cmd, method, url, script_str)
102102

103103
-- This can be quite verbose so let user control it
104104
if config.get("result").show_curl_command then
105-
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command :" .. curl_cmd })
105+
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command: " .. curl_cmd })
106106
end
107107

108108
if config.get("result").show_url then

lua/rest-nvim/init.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rest.setup = function(user_configs)
1818
config.set(user_configs or {})
1919
end
2020

21+
2122
-- run will retrieve the required request information from the current buffer
2223
-- and then execute curl
2324
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
@@ -145,23 +146,31 @@ end
145146
rest.run_request = function(req, opts)
146147
-- TODO rename result to request
147148
local result = req
149+
local curl_raw_args = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
150+
or result.raw
148151
opts = vim.tbl_deep_extend(
149152
"force", -- use value from rightmost map
150153
defaultRequestOpts,
151154
opts or {}
152155
)
153156

154-
-- body =
157+
-- if we want to pass as a file, we pass nothing to plenary
158+
local spliced_body = nil
159+
if not req.body.inline and req.body.filename_tpl then
160+
curl_raw_args = vim.tbl_extend("force", curl_raw_args, {
161+
'--data-binary', '@'..load_external_payload(req.body.filename_tpl)})
162+
else
163+
spliced_body = splice_body(result.headers, result.body)
164+
end
155165

156166
Opts = {
157167
method = result.method:lower(),
158168
url = result.url,
159169
-- plenary.curl can't set http protocol version
160170
-- http_version = result.http_version,
161171
headers = splice_headers(result.headers),
162-
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
163-
or result.raw,
164-
body = splice_body(result.headers, result.body),
172+
raw = curl_raw_args,
173+
body = spliced_body,
165174
dry_run = opts.verbose,
166175
bufnr = result.bufnr,
167176
start_line = result.start_line,

lua/rest-nvim/request/init.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local config = require("rest-nvim.config")
55
-- get_importfile returns in case of an imported file the absolute filename
66
-- @param bufnr Buffer number, a.k.a id
77
-- @param stop_line Line to stop searching
8+
-- @return tuple filename and whether we should inline it when invoking curl
89
local function get_importfile_name(bufnr, start_line, stop_line)
910
-- store old cursor position
1011
local oldpos = vim.fn.getcurpos()
@@ -17,10 +18,13 @@ local function get_importfile_name(bufnr, start_line, stop_line)
1718
if import_line > 0 then
1819
local fileimport_string
1920
local fileimport_line
21+
local fileimport_inlined
2022
fileimport_line = vim.api.nvim_buf_get_lines(bufnr, import_line - 1, import_line, false)
21-
fileimport_string =
22-
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
23-
return fileimport_string
23+
-- check second char against '@' (meaning "dont inline")
24+
fileimport_inlined = string.sub(fileimport_line[1], 2, 2) ~= '@'
25+
fileimport_string = string.gsub(fileimport_line[1], "<@?", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
26+
return fileimport_inlined, fileimport_string
27+
2428
end
2529
return nil
2630
end
@@ -35,10 +39,10 @@ end
3539
-- @return table { external = bool; filename_tpl or body_tpl; }
3640
local function get_body(bufnr, start_line, stop_line)
3741
-- first check if the body should be imported from an external file
38-
local importfile = get_importfile_name(bufnr, start_line, stop_line)
42+
local inline, importfile = get_importfile_name(bufnr, start_line, stop_line)
3943
local lines -- an array of strings
4044
if importfile ~= nil then
41-
return { external = true, filename_tpl = importfile }
45+
return { external = true; inline = inline; filename_tpl = importfile }
4246
else
4347
lines = vim.api.nvim_buf_get_lines(bufnr, start_line, stop_line, false)
4448
end
@@ -59,7 +63,7 @@ local function get_body(bufnr, start_line, stop_line)
5963
end
6064
end
6165

62-
return { external = false, body_tpl = lines2 }
66+
return { external = false; inline = false; body_tpl = lines2 }
6367
end
6468

6569
local function get_response_script(bufnr, start_line, stop_line)

0 commit comments

Comments
 (0)