Uploading Files with curl
When I was reading OpenApi docs about image editing I noticed that they were using -F
arguments. This is something I never seen before, and especially didn't know curl was capable of it.
What Does -F
Do?
The -F
flag tells curl
to send data as multipart/form-data
, the same format browsers use when you upload a file through an HTML form.
A simple example:
curl -F "image=@body-lotion.png" http://example.com/upload
This uploads the body-lotion.png
file under the form field named image
.
What About image[]
?
Sometimes you see a field like image[]
:
curl -F "image[]=@body-lotion.png" http://example.com/upload
The square brackets []
are a convention (especially in PHP and Ruby backend frameworks) that tells the server to treat the incoming files as an array. This is useful when uploading multiple files:
curl -F "image[]=@pic1.png" -F "image[]=@pic2.png" http://example.com/upload
Here, pic1.png
and pic2.png
are both uploaded under the image[]
field, and the server will receive them as a list.
Important: File Location
The file you upload must exist at the path you specify. If it’s just the filename (@body-lotion.png
), it must be in your current working directory. You can also specify full or relative paths:
curl -F "image[]=@/path/to/body-lotion.png" http://example.com/upload
If the file isn’t found, curl
will return an error.