grokking hard

code smarter, not harder

Less well-known uses of curl

Posted at — 2020-Oct-13

When it comes to make HTTP calls, I always use curl as it is a ubiquitous tool for the job.

Today, I discover that I can use curl for some other tasks.

Copying files

curl supports the FILE protocol (file:/), therefore it is possible to “download” a file:

1
2
3
4
$ curl file:/path/to/some/large/file -o /the/destination/file
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2389M  100 2389M    0     0   339M      0  0:00:07  0:00:07 --:--:-- 369M

_(See http://askubuntu.com/questions/17275/progress-and-speed-with-cp)

Querying LDAP

Normally, when I needed to query an LDAP server, ldapsearch is always the de facto tool though it may not be installed on some environment.

Nowadays, I tends to use Docker image for LDAP for the job:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ docker run --rm --name ldap -it --entrypoint bash emeraldsquad/ldapsearch:latest

Inside the container, I use `ldapsearch` for querying:

$ ldapsearch -x -LLL  \
  -h ldap-test-server.example.com \
  -p 8081 \
  -D 'uid=admin,ou=system' \
  -w ${LDAP\_PASSWORD} \
  -b 'ou=users,dc=example,dc=com'

Today, I learned that I can achieve the same task with curl:

1
2
3
$ curl -v \
    -u "uid=admin,ou=system":${LDAP\_PASSWORD} \
    "ldap://ldap-test-server.example.com:8081/ou=users,dc=example,dc=com??sub?(objectclass=*)"

This is really great as curl may come pre-installed on lots of environments whereas ldapsearch and Docker may not.

If you need a more sophisticated query, consider giving LDAP URL Format a read. It will explain the structure of the URL you could use with curl.