Skip to content

An HTTP client for neovim inspired by vscode-restclient and the IntelliJ HTTP client

License

Notifications You must be signed in to change notification settings

blacklight/nvim-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nvim-http

You can use this plugin to run HTTP requests directly in your favourite text editor.

Screen recording of the plugin running an HTTP request

This plugin is inspired by the vscode-restclient extension and the IntelliJ HTTP Client.

It is a more advanced alternative to vim-http, but it comes closer to the VSCode and IntelliJ alternatives because of the following features:

  1. It supports environment variables.
  2. It uses Neovim's async_call mechanism, so the request is not blocking for the editor, and it can be stopped at any time.
  3. An extended syntax for the .http files, with support for JSON and HTML blocks in the requests and the responses, as well as comments.
  4. Real support for multi-request buffers - it matches the request that surrounds the cursor, while supporting visual selection as well.

Installation

Add the plugin to your Neovim configuration through your favourite plugin manager. For example:

Plug 'blacklight/nvim-http'

You may need to run :UpdateRemotePlugins after installing the plugin.

Dependencies

The plugin's core is in Python and it uses the requests library to perform the requests.

The plugin therefore requires:

  • The pynvim bindings
    • pip install pynvim
    • On Arch-based distros: [sudo] pacman -S python-pynvim
    • On Debian-based distros: [sudo] apt install python3-pynvim
    • On Red Hat-based distros: [sudo] yum install python-pynvim
  • The requests Python library
    • pip install requests
    • On Arch-based distros: [sudo] pacman -S python-requests
    • On Debian-based distros: [sudo] apt install python3-requests
    • On Red Hat-based distros: [sudo] yum install python-requests

Usage

Open a buffer and add the body of your HTTP request. If the file name in the buffer has the .http extension, then the plugin will automatically highlight its syntax, and you can also set up nice automation on the file type.

To run your request:

  1. Position the cursor anywhere in the body, or visually select the whole body.
  2. Run the :Http command.

The response will be shown in a vsplit buffer by default. Use the :Http -h if you want to show the response in a horizontal split instead, or :Http -t if you want to show it in a new tab.

The default request timeout is 10 seconds, but you can change it by setting the -T/--timeout flag, e.g. :Http -T 5.

By default the request will follow HTTP redirects, but you can disable this through the --no-redirects flag.

If you want to interrupt the current request, use the :HttpStop command.

Multiple requests

Like the vim-http, this plugin supports running requests both in normal and in visual mode - in the latter case, only the selected range will be interpreted as a request.

However, like the VSCode and IntelliJ plugins, it also supports multiple requests in the same file in normal mode.

Any line starting with the triple hashtag will be interpreted as a request separator:

### The first request

POST {{base_url}}/api/v1/users HTTP/1.1
Authorization: Bearer {{jwt_token}}

{
  "name": "Alice"
}

### The second request

GET {{base_url}}/api/v1/users?name=Alice HTTP/1.1
Authorization: Bearer {{jwt_token}}

### ...more requests

When :Http is run in normal mode, it will detect the boundaries of the surrounding request and only execute that request.

Keyboard shortcuts

You can bind the :Http command to a key combination in your init.vim.

For example, to run the :Http command when pressing Enter in normal mode and in visual mode, if the current buffer contains an .http file:

au FileType http nmap <CR> :Http<CR>
au FileType http vmap <CR> :<C-U>Http<CR>

Environment files

JSON files

This plugin is compatible with environment files generated by the VSCode extension. Any file matching the *.env.json pattern stored in the same folder as the .http file (or the current working directory) will be interpreted as an environment file. Example format:

{
  "dev": {
    "base_url": "http://localhost:3000",
    "jwt_token": null
  },
  "prod": {
    "base_url": "https://foobar.eu-west.some-cloud.com",
    "jwt_token": "SECRET"
  }
}

If multiple environment files are detected, or there are files with more than one environment, then the :Http command will prompt the user for which environment they want to use before executing the request.

.env files

Additionally, the plugin also supports environment variables defined in the .env file in the same folder as the .http file. Note that the .env file doesn't support multiple environments, so it will be used as the default.

A combination of JSON and .env files is also supported. In this case, all the variables from the .env file will be loaded first, and inherited by all the JSON profiles.

Using environment variables

The environment variables can then be wrapped as {{varname}} in the buffer:

GET {{base_url}}/api/v1/users?limit=10
Authorization: Bearer {{jwt_token}}

Inline shell commands

You can also use inline shell commands in the .http file, or in the environment variables. The commands will be executed by the shell and the output will be used in the request.

For example:

###

GET {{URL}}/api/v1/users/$(echo $USER)

###

GET {{URL}}/api/v1/users/me
Authorization: Bearer $(gopass show api/token)
Requested-By: $(whoami)
Requested-At: $(date)

###

Or:

# .env
TOKEN=$(gopass show api/token)
###
# request.http
GET {{URL}}/api/v1/users/me
Authorization: Bearer {{TOKEN}}

About

An HTTP client for neovim inspired by vscode-restclient and the IntelliJ HTTP client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published