Skip to content

Commit 5ebe35f

Browse files
author
Matthieu Coudron
committed
feat: dont display binary output in answer
I was testing an API that returned a zip file and it was getting out of hand so I added an is_binary_content_type to control whether to display the content type. Ideally I would have liked the display to be a default postprocess step so that it gets more generic and let the user more control but this needs deeper refactoring.
1 parent e60aece commit 5ebe35f

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

lua/rest-nvim/curl/init.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ local function create_callback(method, url, script_str)
6363
-- get content type
6464
for _, header in ipairs(res.headers) do
6565
if string.lower(header):find("^content%-type") then
66-
content_type = header:match("application/(%l+)") or header:match("text/(%l+)")
66+
content_type = header:match("application/([-a-z]+)") or header:match("text/(%l+)")
6767
break
6868
end
6969
end
@@ -150,9 +150,15 @@ local function create_callback(method, url, script_str)
150150
end
151151

152152
-- append response container
153-
res.body = "#+RESPONSE\n" .. res.body .. "\n#+END"
153+
local buf_content = "#+RESPONSE\n"
154+
if utils.is_binary_content_type(content_type) then
155+
buf_content = buf_content .. "Binary answer"
156+
else
157+
buf_content = buf_content .. res.body
158+
end
159+
buf_content = buf_content .. "\n#+END"
154160

155-
local lines = utils.split(res.body, "\n")
161+
local lines = utils.split(buf_content, "\n")
156162
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
157163
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)
158164

lua/rest-nvim/request/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local function get_importfile_name(bufnr, start_line, stop_line)
2121
local fileimport_spliced
2222
fileimport_line = vim.api.nvim_buf_get_lines(bufnr, import_line - 1, import_line, false)
2323
fileimport_string =
24-
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
24+
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
2525
fileimport_spliced = utils.replace_vars(fileimport_string)
2626
if path:new(fileimport_spliced):is_absolute() then
2727
return fileimport_spliced
@@ -89,7 +89,6 @@ local function get_body(bufnr, start_line, stop_line, has_json)
8989
end
9090
end
9191

92-
9392
return body
9493
end
9594

lua/rest-nvim/utils/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ math.randomseed(os.time())
55

66
local M = {}
77

8+
M.binary_content_types = {
9+
"octet-stream",
10+
}
11+
12+
M.is_binary_content_type = function(content_type)
13+
return vim.tbl_contains(M.binary_content_types, content_type)
14+
end
15+
816
-- move_cursor moves the cursor to the desired position in the provided buffer
917
-- @param bufnr Buffer number, a.k.a id
1018
-- @param line the desired line

0 commit comments

Comments
 (0)