Insert a REST API Response


Neovim registers and command-line mode can be combined to a pretty decent REST API client. This tip shows how.

First, open a new buffer and add the API request:

Initial Conditions
https://jsonplaceholder.typicode.com/todos/1
NORMALTop1:1
 

Now, yank the current line, which places the contents of the current line into register 0:

Yank the API Request Stringyy
https://jsonplaceholder.typicode.com/todos/1
NORMALTop1:1
 

Finally, lets build the command to call the API and insert the response. First, we learned previously that we can use the :read command to call a shell command and insert its output. The call signature is:

:read !{cmd} [args]

In this case we want to call a REST API, so lets use curl to call the API and receive the response. The command to achieve is:

curl -s '{request}'

where the -s option tells curl to operate in "silent mode". Finally, we could hard-code the request into the command, but we have already yanked it to register 0. We can retrieve register contents in command-line mode using C-R followed by the register name, so our complete command is:

":read !curl -s '<C&minus;r>0'"

Executing this command produces:

Insert the API response
https://jsonplaceholder.typicode.com/todos/1
{
··"userId":·1,
··"id":·1,
··"title":·"delectus·aut·autem",
··"completed":·false
} 
NORMAL88%7:1
6 more lines

if you find yourself using this often, consider creating a macro to save yourself a few keystrokes.