diff options
| author | Ellison <ellisonleao@gmail.com> | 2026-04-10 10:55:57 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-10 09:55:57 -0400 |
| commit | 45f50d238a78314fd575b5a25dd28f14b730cb5b (patch) | |
| tree | 8e53768b440bb8bafb91c0118689d4c0a1dd1cc6 /runtime/lua/vim/net.lua | |
| parent | 6d43869aa0ca8b8795aa91bcc5415dd4f2e2722d (diff) | |
| download | rneovim-45f50d238a78314fd575b5a25dd28f14b730cb5b.tar.gz rneovim-45f50d238a78314fd575b5a25dd28f14b730cb5b.tar.bz2 rneovim-45f50d238a78314fd575b5a25dd28f14b730cb5b.zip | |
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.
Diffstat (limited to 'runtime/lua/vim/net.lua')
| -rw-r--r-- | runtime/lua/vim/net.lua | 29 |
1 files changed, 29 insertions, 0 deletions
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<string, string> ---@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) |