Save your curls

Photo by Onur Buz on Unsplash

Save your curls

The end of grep curl ~/.zsh_history

ยท

2 min read

During work I run a lot of curl commands to test whatever I'm currently developing. Curls are a necessity - because I write lots of REST Endpoints - and since I find it the easiest way to pipe data into whatever function I am writing. Very often, I will use a curl to inject data into any function thereby quickly and safely ensuring things are working smoothly.

I also find myself month after month using the same curls to test areas of our codebase that are heavily used and updated. One of the most annoying things is having to rewrite the same curl from scratch, especially the data, every other month.

There are some good tools for saving, storing, and even running curls - Postman and Insomnia come to mind. But, personally, these tools have too much overhead to use. Because I am lazy, I need something lightweight and efficient to store the curls I use a lot.

Here, I use the simple solution of creating a new MySQL database and table to store the main constituent parts of a curl: the path, the headers, the data, and the method. I added a Python function to execute the SQL so I don't have to write "INSERT INTO curls ..." each time I want to add to my curl collection.

1. Create DB and Table

In MySQL make a new database by:

create database curls;
use curls;

Then make the table:

CREATE TABLE `curls` ( 
    `id` int NOT NULL AUTO_INCREMENT,
    `path` text,         
    `headers` text, 
    `data` text, 
    `method` VARCHAR(255), 
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2. Python Script

Using PyMySQL I create a new database connection and run the INSERT statement:

import sys 
import pymysql.cursors
import pymysql

def main(path, headers, data, method): 
    connect = pymysql.connect( 
        host="127.0.0.1",
        port=int(3306), 
        database="curls",
        user="root",
        password="",
        autocommit=True,
        cursorclass=pymysql.cursors.DictCursor
    )

    with connect.cursor() as curs: 
        curs.execute(
            """
            INSERT INTO curls ( 
                `path`, 
                headers, 
                data,
                method
            )
            VALUES(
                %(path)s, 
                %(headers)s,
                %(data)s, 
                %(method)s
            )
            """, 
            ({ 
                "path": str(path), 
                "headers": str(headers), 
                "data": str(data),
                "method": str(method).upper(), 
            })
        )

main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

3. Run with arguments

Now I just have to run something like:

python3 connect.py  localhost:8888/v1/create-doctor-appts "Content-Type: application/json" '{"start_ts": "2021-10-02T16:33:34.417338-05:00", "length": "60", "doctor": "email@gmail.com"}' POST

And then voila I get:

select * from curls \G
*************************** 1. row ***************************
     id: 1
   path: localhost:8888/v1/retool/validate_appt
headers: Content-Type: application/json
   data: {"start_ts": "2021-10-02T16:33:34.417338-05:00", "length": "60", "doctor": "email@gmail.com"}
 method: POST
1 row in set (0.00 sec)

Now that curl is saved, and I can read, copy, and paste the parts I need.

No more ๐Ÿ™…

grep curl ~/.zsh_history
ย