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
Upload a file as multipart form data using the
-Fflag. curl sends the file as amultipart/form-dataPOST request:curl -F "file=@/path/to/document.pdf" https://api.example.com/uploadThe
@prefix tells curl to read the file contents. Without@, curl sends the literal string.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.pdfUpload 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