curl and multipart/form-data

| 1 min read

In reading through Mr Rob's ix script, I discovered something about curl that I hadn't known about.

The script's key line is this:

url=$(curl -s -F 'f:1=<-' http://ix.io)

My gaze was immediately drawn to this bit: -F 'f:1=<-'. Part of this initially cryptic incantation is actually down to the instructions from the ix.io website itself, in the TL;DR section.

Checking the curl documentation for the -F option, I discover that this venerable command line HTTP client can send multipart/form-data payloads, with files or file contents. So, breaking this incantation down, we have:

PartMeaning
-Fsend a POST with multipart/form-data content
f:1the name of the form field that the website is expecting
<send as file contents (rather than an actual file)
-read the contents from STDIN

And in the context of where this is being executed, STDIN is the ix script's STDIN, in other words, whatever is piped into ix when it's invoked.

In response to the form being POSTed, the ix.io website returns the newly minted unique pastebin URL that was created, and this is saved into the url variable in the script, to be shared in various ways.

Lovely!