7 Changes for Your Curl Upload File

The software industry has many ways to handle file uploads. Users, however, prefer simpler methods. That is why, in this post, we’ll take a look at how to work with Curl upload file. In addition to that, we’ll also look at how to do some advanced things with small changes to the curl command.

What is Curl?

Curl, which stands for client URL, is a free and open-source command-line tool created by hundreds of volunteers and sponsors. Developers use Curl to transmit data to and from a server. Curl functions practically in every OS platform and supports a variety of protocols such as HTTP, FTP, and HTTPS. It started off in 1996 with the name ‘httpget’. Later it was ‘urlget’. Finally, it became ‘Curl’. It first supported the retrieval of currency exchange rates.

Many developers around the world use the Curl tool for the following reasons.

  • It is compatible with every device and OS.
  • You can test endpoints to check their status.
  • It provides detailed sent/received information that is useful for debugging.
  • Curl has excellent error logging

At its most basic level, Curl allows you to communicate with a server by defining the destination along with the data you wish to transmit.

How do I Install Curl?

When it comes to installation, Curl differs from operating system to operating system. Let’s see how it’s done in macOS, Linux, and Windows.

macOS

On macOS, cURL comes pre-installed. If you happen to install it manually run the following two commands in the terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
brew install curl

Linux

On Linux, cURL also comes pre-installed. If that’s not the case, then you can install it by running the following command.
sudo apt-get install curl

Windows

cURL comes pre-installed on the latest Windows editions. If it’s not installed, however,  you can go to http://curl.haxx.se/download.html and download either Win32 or Win64 zip file and follow the steps in the documentation.

How do I Implement a Curl File Upload?

Curl is an excellent tool for making server requests and uploading files. Many individuals make the mistake of assuming that using -X POST as standard form data will upload files in CURL. However, this may cause issues. Use the -d command-line option to upload a file as shown below.

curl -d @path/to/data.json UPLOAD_ADDRESS\

Curl sends the appropriate MIME data type based on the file extension. It also automatically provides the Content-Type: application/json HTTP header along with your request if you send a JSON file using the cli -d @data.json options.

What Are 7 Changes That’ll Make a Big Difference With Your Curl File Upload?

You can use this command if your only objective is to simply upload a file using Curl. Let’s see what Curl provides us if you want to do more than that.

Can I send data with a different type?

Curl provides an option to send data with different content types. You can specify the content type you want with -H command.

curl https://reqbin.com/echo/post/json -d @path/to/data.json -H "Content-Type: application/javascript"

The default content type here is application/json, we have changed it to application/javascript.

Can I upload files with multipart/form data?

The -F (— form) command is the way to upload a file in multipart/form-data format using CURL.

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

Let’s see a more complicated example.
curl -F 'file=@/home/myuser/doc.pdf’ -v UPLOAD_ADDRESS

Here, we upload the doc.pdf file to the relevant server address. However, we give the ‘-v’ option only for debugging purposes. You could remove it in a production environment.

Can I upload multiple files?

The previous example only supports single file uploads. If you want to support multiple file uploads, use the -F option.
curl -F ‘data=@path/to/local/file’ -F ‘data=@path/to/local/file2’ UPLOAD_ADDRESS

Can I upload files without any changes?

Curl’s default behavior is to strip line-feed characters/carriage returns. So if you want to upload the file without any changes, use the –data-binary option.
curl -X POST --data-binary @path/to/my-file.txt UPLOAD_ADDRESS

Can I upload a directory with files?

To repeatedly upload all files from a specified directory, use curl with ‘find’. You can use the following command to upload a directory with files in it. Curl, however, creates a directory on the server itself in order to support the file uploads.
find dist -type f -exec curl -u username@my_server.com:my_password --ftp-create-dirs -T {} UPLOAD_ADDRESS \;

Can I upload a file with a comma in its name?

As an example, assume your filename is ‘Hello, text file.txt’. In order to upload this file use the following methods”

    • Put the filename in quotation marks (make sure to escape the quotation marks):

curl --progress-bar -F "fileUpload=@\"Hello, text file.txt\""

    • Escape the comma

curl --progress-bar -F "fileUpload=@Hello\, text file.txt"

How do I simply transfer a file from one location to another?

You can use the following command to simply transfer a file from one place to another. Use the -F command to submit a file to an endpoint accepting @Post requests.
curl -u username:password --upload-file file1.txt UPLOAD_ADDRESS

Who is using Filestack for file uploads?

Teachable (formerly Fedora) is a New York-based startup, founded by Ankur Nagpal and Conrad Wadowski. Described as “Shopify for the educational market”, Teachable is an e-commerce platform that enables teachers to create their own online schools and sell class content to students through their own personal websites.

“All of our teachers now use Filestack as their primary tool to get their course content loaded onto our system. This includes but is not limited to videos, images, text files and icons. All of it runs through Filestack,” says Nagpal. “We chose Filestack for the uploading option as we simply did not want to deal with it or build it ourselves. It is better to get someone specialized in this area to do it. We did not have to think about the process. We had the direct link and it just worked automatically.”

Read the file Teachable case study.

Ready to Enjoy a Seamless File Upload Experience Filestack?

Curl lets you upload files in many ways using CLI commands. Another step beyond Curl is to use something like PHP or Flash for uploads. However, the days of writing and memorizing CLI commands are over. If you’re ready to start building powerful file upload solutions, then sign up with Filestack. Filestack offers a robust set of technology tools and API to improve your file upload, transformation, and delivery performance. The platform gets user content from anywhere and improves the file upload experience with a powerful, easy-to-use API.

Don’t waste time! Head over to Filestack and sign up for free to start enjoying the best file uploading experience!

Read More →