Skip to content

Commit 0d4b617

Browse files
authored
Merge pull request #211 from teto/stringify_request
2 parents aea7c64 + 09143ad commit 0d4b617

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

lua/rest-nvim/init.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ end
1717
rest.run = function(verbose)
1818
local ok, result = request.get_current_request()
1919
if not ok then
20+
log.error("Failed to run the http request:")
21+
log.error(result)
2022
vim.api.nvim_err_writeln("[rest.nvim] Failed to get the current HTTP request: " .. result)
2123
return
2224
end
@@ -53,11 +55,19 @@ rest.run_file = function(filename, opts)
5355
return true
5456
end
5557

58+
59+
-- run will retrieve the required request information from the current buffer
60+
-- and then execute curl
61+
-- @param req table see validate_request to check the expected format
62+
-- @param opts table
63+
-- 1. keep_going boolean keep running even when last request failed
5664
rest.run_request = function(req, opts)
5765
local result = req
5866
opts = vim.tbl_deep_extend(
5967
"force", -- use value from rightmost map
60-
{ verbose = false }, -- defaults
68+
{ verbose = false,
69+
highlight = false
70+
}, -- defaults
6171
opts or {}
6272
)
6373

@@ -81,7 +91,7 @@ rest.run_request = function(req, opts)
8191
LastOpts = Opts
8292
end
8393

84-
if config.get("highlight").enabled then
94+
if opts.highlight then
8595
request.highlight(result.bufnr, result.start_line, result.end_line)
8696
end
8797

lua/rest-nvim/request/init.lua

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,44 @@ M.buf_get_request = function(bufnr, curpos)
359359
end
360360

361361
M.print_request = function(req)
362+
print(M.stringify_request(req))
363+
end
364+
365+
-- converts request into string, helpful for debug
366+
-- full_body boolean
367+
M.stringify_request = function(req, opts)
368+
opts = vim.tbl_deep_extend(
369+
"force", -- use value from rightmost map
370+
{ full_body = false,
371+
headers = true
372+
}, -- defaults
373+
opts or {}
374+
)
362375
local str = [[
363-
version: ]] .. req.url .. [[\n
376+
url : ]] .. req.url .. [[\n
364377
method: ]] .. req.method .. [[\n
365-
start_line: ]] .. tostring(req.start_line) .. [[\n
366-
end_line: ]] .. tostring(req.end_line) .. [[\n
367-
]]
368-
print(str)
378+
range : ]] .. tostring(req.start_line) .. [[ -> ]] .. tostring(req.end_line) .. [[\n
379+
]]
380+
381+
if req.http_version then
382+
str = str .. "\nhttp_version: " .. req.http_version .. "\n"
383+
end
384+
385+
if opts.headers then
386+
for name, value in pairs(req.headers) do
387+
str = str .. "header '" .. name .. "'=" .. value .. "\n"
388+
end
389+
end
390+
391+
if opts.full_body then
392+
if req.body then
393+
local res = req.body
394+
str = str .. "body: " .. res .. "\n"
395+
end
396+
end
397+
398+
-- here we should just display the beginning of the request
399+
return (str)
369400
end
370401

371402
local select_ns = vim.api.nvim_create_namespace("rest-nvim")

0 commit comments

Comments
 (0)