How to send files with curl using either HTTP via -F, or FTP/SMTP via -T
TL;DR
Just need to copy the example so you can get back to work? Here you go!
HTTP on a Unix-like system:
curl -F "form_field_name=@/path/to/file" "URL"
HTTP on Windows:
curl -F "form_field_name=@C:\path\to\file" "URL"
FTP/SMTP
curl -T file https://example.com
The "curl -F" explanation
By now we've all seen and used curl -F
before, but why is it -F
?
Well, let's start out by seeing what man curl
says about uploading a file:
-F, --form <name=content>
Example: send an image to an HTTP server, where 'profile' is the name of the form-field to which the file portrait.jpg is the input:
curl -F profile=@portrait.jpg https://example.com/upload.cgi
Example: send your name and shoe size in two text fields to the server:
curl -F name=John -F shoesize=11 https://example.com/
Turns out that we're sending the file the same way a browser would upload it!
And as you can see, -F
isn't exclusive to files since it's actually sending Form data!
This explains why we need to provide a form_field_name
and if that isn't correct then
your "form submission" will fail.
NOTE: You can also send multiple files via
-F
like so:
curl -F "driversLicenseFront=@/path/to/file1" -F "driversLicenseBack=@/path/to/file2" "URL"
What about FTP/SMTP?
Curl also has the ability to upload files directly to FTP or SMTP servers via the -T
or --upload-file
arg.
The man page for this provides some good examples:
curl -T file https://example.com
curl -T "img[1-1000].png" ftp://ftp.example.com/
curl -T file -T file2 https://example.com https://example.com
curl --upload-file "{file1,file2}" https://example.com
Conclusion
We all use curl in our careers, and we all forget what the exact command line arguments are, that's why we wrote this guide.
There are more ways to use curl to upload files, but these are by far the most common. If we missed anything then please let us know and we'll add/fix it ASAP!