Curl upload file allows you to send data to a remote server. The command-line tool supports web forms integral to every web system, including multipart form data and file input fields.
When you execute a curl file upload [1] for any protocol (HTTP, FTP, SMTP, and others), you transfer data via URLs to and from a server. In this post, I’ll guide you through uploading file data using the HTTP protocol on curl successfully, including how to handle different file types like binary files and array files. But before we dive into the nitty-gritty…
What is curl?
Curl stands for client URL. It is a command-line tool built to send data from or to a remote server using various network protocols such as HTTP, FTP, FILE, IMAP, SBM, SMTP, and others. It is capable of handling various file extensions and making curl post file requests.
Curl runs on Windows, Linux, and macOS platforms. It also has built-in support for HTTP cookies, SSL, user authentication, proxies, and certificate validation.
Since 1998, developers use curl as a command-line tool and library for transferring data with URLs. It works as both a separate console application as well as compiled as a dynamic library into other programs, capable of handling different file structures, including binary files, image files, and more.
You also can use curl to download or upload files, including array files, submit web forms, send a curl post file request to API endpoints, and simulate user actions without using a web browser.
How to install curl?
You can install and use curl on Windows, macOS, and Linux operating platforms.
Installing curl on Windows
For Windows, follow these steps:
- Download the curl Windows installer from the curl official website [1] (64-bit recommended).
- Unzip the Curl.zip file into a folder on your computer (i.e., C:\Curl).
- Invoke the curl command from any folder, including the curl installation folder (C:\Curl\bin), to your Windows PATH environment variable.
- Enter
curl –version
on the Windows command line to check if it was installed correctly and ensure you can use curl commands.
If you installed it correctly, you’ll get this response:
curl --version
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL
Installing curl on macOS
For macOS, curl comes pre-installed on the operating system.
You can decide to update to the latest curl version by installing macOS Homebrew Software Package Manager.
After installing the Homebrew Package Manager, open a terminal and type:
brew install curl
If installed properly, you’ll get the response below when you type curl –version:
curl 7.31.0 (x86_64-apple-darwin12.4.0) libcurl/7.31.0 OpenSSL/0.9.8x zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: IPv6 Large File NTLM NTLM_WB SSL libz
Installing curl on Linux/Ubuntu
For Linux/Ubuntu, you need to enter the following command in a terminal.
sudo apt install curl
or
sudo apt-get install curl
And to verify if CURL is correctly installed and working, run the curl –version command in a terminal. If positive, you’ll get the response below:
curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp 2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
You can learn more about curl, its installation, and its usage here [2].
Guide to curl file upload
Uploading files using curl is pretty straightforward once you’ve installed it.
Several protocols allow curl file upload including: FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, and TFTP.
Each of these protocols works with curl differently for uploading data.
For HTTP (and HTTPS), it offers several ‘uploads’ methods to upload data to a server. Curl provides easy command-line options to do it in the three common ways:
- POST
- Multipart form post
- PUT
Let’s take a look at the HTTP protocol and several ways you can execute curl upload data with it!
Uploading a file with curl using HTTP POST
POST is an HTTP file upload method created to send data to a receiving web application. Many HTML forms on the web rely on this method to work.
POST method uses the -d or –data options to deliver a chunk of relatively small amounts of data to the remote server (and receiver).
After data has been filled in a form, a browser URL encodes and sends it as a serialized name=value pairs separated with ampersand symbols (&).
curl -d 'name=admin&shoesize=12' http://example.com/
Send the data using curl’s –d or –data option.
To execute a curl file upload, you need to use the –d command-line option and begin data with the @ symbol.
The file’s name should come after the data with @ symbol so curl can read it and send it to the server.
Example:
curl -d @path/to/data.json https://reqbin.com/echo/post/json
Are you sending data with a custom content type?
Use the –H command-line option instead and specify your content type.
Example:
curl https://reqbin.com/echo/post/json
-d @path/to/data.json
-H "Content-Type: application/javascript
You can read more about the curl HTTP POST file upload method here [3].
Multipart form post
HTTP Multipart form posts are also widely used to upload files in HTML forms on the web. Do this using the –F data option.
This file upload method is also an HTTP POST but it sends the data formatted differently according to some rules. This is where the term “multipart” comes into play.
Since the data format the server receives is different, you can’t pick a type of POST to use. This depends on the server; on what it expects and can handle.
To send an HTML form with curl multipart form posts, you need to add each multipart with one –F (or –form) option and add one –F for every input field in the form.
curl -F person=anonymous -F secret=@file.txt http://example.com/submit.cgi
You can read more about the HTTP multipart form post method here [4].
PUT
PUT is the least used curl file upload HTTP method in HTML forms on the web, and a lot of servers don’t have PUT enabled.
It delivers complete resources meant to be sent as-is on the remote site. Or it could even replace an existing resource there.
To send files using the PUT method, you need to use the –T data option.
curl -T uploadthis http://example.com/
You can read more about the HTTP PUT method here [5].
Should I Use curl Upload File?
If you’re looking for seamless file upload between your browser and a remote server, you should consider using curl.
Curl is a command-line tool and library for transferring data from and to remote servers.
It works well with web forms and is a great fit for file upload on your web application.
Are you ready to start enjoying a seamless curl upload file experience?
Then, you need to start with Filestack.
Can Filestack Streamline the Process of doing File Uploads?
Filestack is a solution that offers a set of robust technology tools and API to improve your file upload experience.
It features a powerful, easy-to-use API to fetch user content from anywhere and improves your file upload experience.
When it comes to fast and seamless file uploads, URL ingestion, or iOS/Android device integration, Filestack has got you covered!
Head over to Filestack and sign up for free.
Your journey to a seamless file uploading experience starts here!
[1] https://curl.se/download.html
[2] https://curl.se/
[3] https://everything.curl.dev/http/post
[4] https://everything.curl.dev/http/multipart
[5] https://everything.curl.dev/http/put
Filestack is a dynamic team dedicated to revolutionizing file uploads and management for web and mobile applications. Our user-friendly API seamlessly integrates with major cloud services, offering developers a reliable and efficient file handling experience.
Read More →