Quick and dirty CURL use

Quickly run complex HTTP requests from terminal.

Sometimes I want to execute an HTTP request against a website or API, but with some changes. Maybe I am developing a new feature, or doing exploratory testing, or maybe just some hacking. Making a request is not that simple. With cookies and tokens, the request cannot be written from scratch anymore. The requests are so much longer than GET /foo that even my favorite terminal tool httpie does not help.

Here is an example. I have a small repo express-sessions-tutorial showing sessions / CSRF token setup. There are only two pages: index and form. The index page sets the session cookie, and the form page sets both the cookie and a unique token to prevent replay attack. Just clone the repo, install the dependencies and start the server npm start.

git clone [email protected]:bahmutov/express-sessions-tutorial.git
cd express-sessions-tutorial
npm install
npm start

Then open localhost:3000

Index and form pages

Can I increment the session counter by loading the page from the terminal? Well, I need to send the GET / request, but with session token. The request in DevTools is giant!

Index page request

Instead of typing the cookies by hand, you can copy the entire request right from the DevTools Network panel.

Copy request as CURL

But what if we want to modify the request in the terminal? Can we load the form page for example with the same cookie? And how to do this quickly? Entering all the info into a full fledged HTTP client app like Postman is just too much effort.

Luckily, terminal has a quick edit and run last command feature. Here is a screenshot of the terminal where we first run ls command and then edit the previous command to replace ls with cat and run it again.

Quickly edit and run last command

We can use the same ^text^new text to change the loooooong curl command. For example we can go the form page as a user from the index page.

Run updates CURL command

Mission accomplished.