luahttprequestneovimneovim-plugin

Can I perform a GET request / fetch inside of a neovim plugin?


First of all, I'm a complete neovim plugin noob. But I had an idea for my first one that would involve pinging a website to see if the latest version of software matches what is currently installed on the system, and alerting the user if there is a new version for them to install. I just have exhausted my google-fu in regards to this.

Ideally I'd like to write something like (psuedo-code INCOMING!):

local http = require('http')
local json = require('json')

local latest = http.request("/url/to/latest/version")
print(json.parse(latest))

I've gotten relatively far with this, even so far as printing out the latest version and the currently installed one. It just involved installing a lot of extra stuff (luarocks, socket.http), and I have no idea how a user installing this nvim plugin via plugin manager would have it "just work."

Any information provided would be extremely helpful! Thanks for your time.


Solution

  • Don't make it look harder than it is. Just exec curl or wget through io.popen and parse with the help of a builtin JSON decoder.

    local h = io.popen("wget -qO- https://example.org/example.json")
    local rawdata = h:read("all")
    h:close()
    local t = vim.json.decode(rawdata)
    print(vim.inspect(t))
    

    And if you need some extra modules then put them all under your plugin's /lua subdirectory.