nvim_runtime_lua

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) -> nil
function M.request(method: vim.net.HttpMethod, url: string, opts: vim.net.request.Opts, response: vim.net.request.ResponseFunc) -> nil
Parameters
methodvim.net.HttpMethod

(default: GET) The HTTP method (GET, POST, PUT, PATCH, HEAD, DELETE).

urlstring

The URL for the request.

on_responsevim.net.request.ResponseFunc | nil

Callback invoked on request completion.

Returns
{ close: fun() }

Object with close() method which cancels the request.