DEV Community

Rajasegar Chandran
Rajasegar Chandran

Posted on • Edited on

Helix editor: Make HTTP requests and insert JSON

In this post we are going to take a look at how to make http requests with Helix editor and insert the resulting JSON data automatically inside a buffer.

Helix

Helix is a postmodern text editor built in Rust built for the terminal. It is inspired by Kakoune. Helix has got multiple selections, built-in Tree-sitter integration, powerful code manipulation and Language server support.

One of the best thing about Helix is the no-plugin philosophy, which is also something unfortunate. You might not need any plugins, because you already have all the features built in like Fuzzy finder to jump to files and symbols, project wide search, beautiful themes, auto closing bracket pairs, surround integration and more. All you need is to just enable and tweak them in the config file.

Even though there is no plugin support for Helix yet, that doesn't stop you from using and extending this awesome editor using CLI tools and shell scripts. And that's exactly what we are going to do in this post.

We are going to use a shell script written around the curl command to insert JSON data from the jsonplaceholder api.

Inserting output from shell commands

In Helix, you can run a shell command and insert the resulting output before each selection in a buffer. This is how it is done. You call the :insert-output command by pressing : and type insert-ouput followed by your shell command. Or you can simply press ! key binding in Normal mode to automatically put you in the command mode with the insert-output command.

:insert-output curl -s -X GET https://jsonplaceholder.typicode.com/todos
Enter fullscreen mode Exit fullscreen mode

The above command will make the HTTP call and put the resulting output inside your buffer.

Shell script

Now we don't have to type the full curl command everytime with all the options and parameters. What we are going to do is to create a shell script and run it by just getting the last segments of the url.

If you are in a Linux based system like Ubuntu, go to the folder /usr/local/bin or on MacOS go to ~/.local/bin

touch curlget
hx curlget
Enter fullscreen mode Exit fullscreen mode

You might not have admin permissions, in that case please prepend the commands with sudo

Type in the following contents in the newly created file.

#!/bin/sh

curl -s -X GET "https://jsonplaceholder.typicode.com/$1"
Enter fullscreen mode Exit fullscreen mode

The $1 is a placeholder for the first argument you pass to this shell script.

The last thing is to give executable permissions for the newly created shell script.

chmod +x curlget
Enter fullscreen mode Exit fullscreen mode

Now you can call the shell script from your command line like this:

curlget todos
Enter fullscreen mode Exit fullscreen mode

Calling the newly created shell script from Helix, press ! and type curlget todos and press Enter.

:insert-output curlget todos
Enter fullscreen mode Exit fullscreen mode

Once this command is executed you will get the JSON result as something below:

Image description

You can get a particular todo record by passing the id like this:

:insert-output curlget todos/1
Enter fullscreen mode Exit fullscreen mode

Image description

You can also get other resources and use query parameters

:insert-output curlget comments?postId=1
Enter fullscreen mode Exit fullscreen mode

Hope you enjoyed this post. The shell script is in nascent stage only, you can iteratively refine it to send more HTTP requests like POST, PUT and DELETE. Or you can make use of the awesome command line utilities available to make HTTP requests like HTTPie, HTTP Prompt and ain.

Please let me know your thoughts in the comments section on how we can improve the workflow or the shell script.

Top comments (0)