From 45f50d238a78314fd575b5a25dd28f14b730cb5b Mon Sep 17 00:00:00 2001 From: Ellison Date: Fri, 10 Apr 2026 10:55:57 -0300 Subject: feat(vim.net): custom request() headers #38837 Problem Cannot specify headers in vim.net.request() call. Solution Support opts.headers in vim.net.request opts. --- runtime/lua/vim/net.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'runtime/lua/vim/net.lua') diff --git a/runtime/lua/vim/net.lua b/runtime/lua/vim/net.lua index 5919547be7..d8bf5f1eb7 100644 --- a/runtime/lua/vim/net.lua +++ b/runtime/lua/vim/net.lua @@ -14,6 +14,11 @@ local M = {} --- ---Buffer to save the response body to. ---@field outbuf? integer +--- +---Table of custom headers to send with the request. +---Supports basic key/value header formats and empty headers as supported by curl. +---Does not support @filename style, internal header deletion (e.g: 'Header:'). +---@field headers? table ---@class vim.net.request.Response --- @@ -47,6 +52,11 @@ local M = {} --- outbuf = 0, --- }) --- job:close() +--- +--- -- Add custom headers in the request +--- vim.net.request('https://neovim.io/charter/', { +--- headers = { Authorization = 'Bearer XYZ' }, +--- }) --- ``` --- --- @param url string The URL for the request. @@ -76,6 +86,25 @@ function M.request(url, opts, on_response) vim.list_extend(args, { '--output', opts.outpath }) end + if opts.headers then + vim.validate('opts.headers', opts.headers, 'table', true) + for key, value in pairs(opts.headers) do + if type(key) ~= 'string' or type(value) ~= 'string' then + error('headers keys and values must be strings') + end + + if key:match(':$') or key:match(';$') or key:match('^@') then + error('header keys must not start with @ or end with : and ;') + end + + if value == '' then + vim.list_extend(args, { '-H', key .. ';' }) + else + vim.list_extend(args, { '-H', key .. ': ' .. value }) + end + end + end + table.insert(args, url) local job = vim.system(args, {}, function(res) -- cgit