Uploading files over HTTP POST, multipart forms, or FTP is common. cURL supports these methods and simplifies file transfers, whether for images, documents, or binary data.

By using --form, files can be sent as multipart/form-data. For raw transfers or FTP, cURL manages content types and authentication automatically.

Mastering file uploads with cURL aids integration with APIs, web services, and remote servers. Automation and scripting reduce manual effort and enable rapid development workflows.

Steps to upload files using cURL:

  1. Open a terminal session with access to the target files.
    $ whoami
    user
  2. Verify that cURL is installed and review the supported protocols.
    $ curl --version
    curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11
    Release-Date: 2022-01-05
    Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s smtp smtps telnet tftp
    Features: AsynchDNS HTTPS-proxy IPv6 Largefile libz NTLM SSL TLS-SRP UnixSockets
    ##### snipped #####

    If cURL is missing, install the curl package on Ubuntu using

    $ sudo apt update && sudo apt install --assume-yes curl

    .

  3. Upload a single file to an HTTP endpoint as multipart/form-data using the --form option.
    $ curl --form "file=@/home/user/example.txt" https://httpbin.org/post
    {
      "files": {
        "file": "example-content"
      },
      "form": {},
    ##### snipped #####
    }

    --form sends the file field with multipart/form-data, matching typical HTML upload forms.

  4. Send additional files or perform an FTP upload when required by the remote service.
    $ curl --form "file1=@/home/user/file1.txt" --form "file2=@/home/user/image.jpg" https://httpbin.org/post
    
    $ curl --upload-file "/home/user/example.txt" ftp://ftp.example.com/incoming/ --user username:password

    Plain FTP and basic authentication expose credentials and data to the network, so encrypted alternatives such as FTPS or SFTP are recommended for sensitive transfers.

  5. Confirm successful uploads by inspecting the HTTP status and response body or monitoring the remote endpoint.
    $ curl --form "file=@/home/user/example.txt" https://httpbin.org/post --verbose
    *   Trying 3.222.190.64:443...
    * Connected to httpbin.org (3.222.190.64) port 443
    > POST /post HTTP/1.1
    > Host: httpbin.org
    ##### snipped #####
    < HTTP/1.1 200 OK
    ##### snipped #####

    Success signals include an HTTP status such as 200 OK in verbose output, a response body indicating the file field is present, or verification on the remote service that the uploaded file exists.

Discuss the article:

Comment anonymously. Login not required.