How to upload a file with curl

Upload files via HTTP POST multipart form data or PUT request body using curl.

How to upload a file with curl

Upload files via HTTP POST multipart form data or PUT request body using curl.

Prerequisites

  • curl installed.
  • A file to upload and a server endpoint that accepts file uploads.

Step-by-Step: Upload a File with curl

  1. Upload a file as multipart form data using the -F flag. curl sends the file as a multipart/form-data POST request:

    curl -F "file=@/path/to/document.pdf" https://api.example.com/upload

    The @ prefix tells curl to read the file contents. Without @, curl sends the literal string.

  2. Upload a file using a PUT request body. curl sends the raw file content:

    curl -X PUT -T /path/to/document.pdf https://api.example.com/files/document.pdf
  3. Upload with a custom Content-Type header:

    curl -X POST -H "Content-Type: application/octet-stream" --data-binary @/path/to/file.bin https://api.example.com/upload

How to Verify the Upload Succeeded

Check the HTTP response status code. A successful upload returns 200 or 201:

curl -F "file=@report.pdf" -w "\nHTTP Status: %{http_code}\n" https://api.example.com/upload