Upload files with CURL

Pete Houston
Nov 22, 2015 · 1 min read

CURL is a great tool for making requests to servers; especially, I feel it is great to use for testing APIs.

To upload files with CURL, many people make mistakes that thinking to use -X POST as regular form data; in facts, that way will cause errors.

The proper way to upload files with CURL is to use -F ( — form) option, which will add enctype=”multipart/form-data” to the request.

$ curl -F ‘data=@path/to/local/file’ UPLOAD_ADDRESS

For example, if I want to upload a file at /home/petehouston/hello.txt to the server http://localhost/upload which processes file input with form parameter named img_avatar, I will make request like this,

$ curl -F 'img_avatar=@/home/petehouston/hello.txt' http://localhost/upload

Upload multiple files

To send upload request for multiple files, just add an additional -F option,

$ curl -F 'fileX=@/path/to/fileX' -F 'fileY=@/path/to/fileY' ... http://localhost/upload

Upload an array of file

To send upload request for an array file, simply put additional -F options with same form parameter name as array,

$ curl -F 'files[]=@/path/to/fileX' -F 'files[]=@/path/to/fileY' ... http://localhost/upload

Yay, that’s easy, have fun :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store