brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -F uploads a file the way an HTML form does

Curl
~/tips/tip-curl-form-upload-file

curl -F '[email protected];type=image/jpeg' \

Send a multipart form with a file field, a text field and a content type.

Upload forms and many APIs expect multipart/form-data, not JSON. -F builds that body for you, one field per flag.

An @ attaches a file as an upload, and plain text is a normal field:

curl -F '[email protected];type=image/jpeg' \
     -F 'title=Holiday' \
     https://example.com/upload

curl sets the method to POST, writes the boundary, and adds a Content-Disposition header with the original file name for each part. The ;type= part sets the content type of that one part; without it curl guesses from the extension and falls back to application/octet-stream. You can also rename the file the server sees with ;filename=cover.jpg.

There is a second prefix that looks similar: < reads a file into a plain text field instead of attaching it. -F 'notes=<notes.txt' sends the contents as a field value, without a file name.

Watch out with values you do not control. A value that starts with @ or < is treated as a file to read, which can leak a local file into the upload. For text from user input or variables I’d use --form-string, which sends the value literally. The upload goes as one request, so for big files a server limit like a maximum body size can still reject it.

Further reading: curl man page, everything curl: multipart formposts

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link