vim.net
Methods1
function M.request(method: vim.net.HttpMethod, url: string, opts: vim.net.request.Opts | nil, on_response: vim.net.request.ResponseFunc | nil) -> { close: fun() }
Makes an HTTP request to the given URL, asynchronously passing the result to the specified on_response, outpath or outbuf.
Examples:
-- Write response body to file.
vim.net.request('https://neovim.io/charter/', {
outpath = 'vision.html',
})
-- Process the response.
vim.net.request(
'https://api.github.com/repos/neovim/neovim',
{},
function (err, res)
if err then return end
local stars = vim.json.decode(res.body).stargazers_count
vim.print(('Neovim currently has %d stars'):format(stars))
end
)
-- Write to both file and current buffer, but cancel it.
local job = vim.net.request('https://neovim.io/charter/', {
outpath = 'vision.html',
outbuf = 0,
})
job:close()
-- Add custom headers in the request.
vim.net.request('https://neovim.io/charter/', {
headers = { Authorization = 'Bearer XYZ' },
})
-- POST request with body.
vim.net.request('POST', 'https://example.com/api', {
body = '{"key": "value"}',
headers = {['Content-Type'] = 'application/json' }
})
Overloads
function M.request(url: string, opts: vim.net.request.Opts, response: vim.net.request.ResponseFunc) -> nilfunction M.request(method: vim.net.HttpMethod, url: string, opts: vim.net.request.Opts, response: vim.net.request.ResponseFunc) -> nilParameters
methodvim.net.HttpMethod(default: GET) The HTTP method (GET, POST, PUT, PATCH, HEAD, DELETE).
urlstringThe URL for the request.
optsvim.net.request.Opts | nilon_responsevim.net.request.ResponseFunc | nilCallback invoked on request completion.
Returns
- { close: fun() }
Object with
close()method which cancels the request.