Upload a File with curl Using PUT, a Form or Raw Data
Upload a file with curl -T for a PUT, -F for a multipart form field, or --data-binary @file for a raw POST body. A URL ending in a slash gets the file name appended.
You have a file on disk and an endpoint that wants it, and the three options curl offers send it in three different shapes.
echo hello > note.txt
curl -s -T note.txt localhost:8000/up/
curl -s -F [email protected] localhost:8000/form | grep -o '^POST /form\|filename="[^"]*"'
curl -s --data-binary @note.txt localhost:8000/rawPUT /up/note.txt body=b'hello\n'
POST /form
filename="note.txt"
POST /raw body=b'hello\n'-T note.txt sends a PUT with the file as the body, and because the URL ended in a slash curl added the file name, so the server saw PUT /up/note.txt. -F [email protected] builds a multipart form and sends a POST, which is what an HTML file input produces, and the server saw filename="note.txt". --data-binary @note.txt posts the raw bytes, newline included. The choices are explained under --upload-file and --form.
Match the shape to what the server expects, because a PUT body and a multipart field are not interchangeable. Plain -d @file strips carriage returns and newlines, so use --data-binary for anything that is not form data. Add -H 'Content-Type: ...' when the endpoint checks it.