Send Form Data with curl Using -d or -F
Send form fields with curl -d for urlencoded bodies and -F for multipart. A plus sign sent with -d arrives as a space, so encode values with --data-urlencode.
A form field that holds c++ arrives at the server as c, and the plus signs are gone.
curl -s -d name=ada -d 'lang=c++' http://127.0.0.1:8000/form | sed -n '2p;4p'
echo ---
curl -s -d name=ada --data-urlencode 'lang=c++ & more' http://127.0.0.1:8000/form | sed -n '2p;4p'
echo ---
curl -s -F name=ada -F note=hi http://127.0.0.1:8000/form | sed -n '2p;4,$p'Content-Type: application/x-www-form-urlencoded
body: name=ada&lang=c++
---
Content-Type: application/x-www-form-urlencoded
body: name=ada&lang=c%2B%2B+%26+more
---
Content-Type: multipart/form-data
body: --BOUNDARY
Content-Disposition: form-data; name="name"
ada
--BOUNDARY
Content-Disposition: form-data; name="note"
hi
--BOUNDARY---d sends each pair exactly as typed, joined with an ampersand, and a form parser decodes a plus sign as a space and an ampersand as the next field. --data-urlencode percent-encodes the value, so the plus signs and the ampersand in the second command survive. -F builds a multipart body, with one part per field, which is what a file upload needs. The behavior of each is in the curl manual. The echo server replaces the random boundary with a fixed word so the output is stable.
Use -F only when the server expects multipart/form-data; many JSON APIs and simple form handlers accept only the urlencoded kind. With -F, a value that begins with an at sign is read as a file name, so send literal text that starts with one through --form-string. Combining -d and -F in one command is an error.