Upload files with CURL

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 🙂


No Responses

Write a response