A Step-By-Step Guide To A CURL Upload File

A Step-By-Step Guide To A CURL Upload File

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.

Windows

For Windows, follow these steps:

  1. Download the CURL Windows installer from the CURL official website [1] (64-bit recommended).
  2. Unzip the Curl.zip file into a folder on your computer (i.e., C:\Curl).
  3. Invoke the CURL command from any folder, including the CURL installation folder (C:\Curl\bin), to your Windows PATH environment variable.
  4. 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

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

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 from posts
  • PUT

Let’s take a look at the HTTP protocol and several ways you can execute CURL upload data with it!

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 e –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

Read More →