Skip to content
shellhacks.dev
Network

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.

2 min read
Summarize in AI

A form field that holds c++ arrives at the server as c, and the plus signs are gone.

Shell
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'
Text
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.

Taggedcurlhttp
Share

More snippets

Network

Send JSON in a POST Request with curl

Send JSON with curl -H 'Content-Type: application/json' -d '{...}'. Plain -d labels the body as a form, and curl 7.82 added --json to set both headers.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.