Curl POST Form Example

HTML forms post in one of two encodings, and curl has a flag for each. Use the one the form's enctype names, or the server will not find your fields.

Last updated . How this page is checked.

The command

curl -d "email=ada@example.com" -d "plan=starter" https://httpbin.org/post

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-d name=valueOne URL-encoded field. Repeat it; curl joins the pieces with &.
--data-urlencode name=valueLike -d, but curl percent-encodes the value for you.
-F name=valueOne part of a multipart/form-data body.
-F name=@pathA file part, as a file input would send.

URL-encoded forms

A form with no enctype attribute posts application/x-www-form-urlencoded: name=value pairs joined with ampersands, with special characters percent-encoded. Plain -d does not encode anything, so a value containing &, =, + or a space is misread by the server. Use --data-urlencode for any value you did not write yourself.

curl https://httpbin.org/post \
  --data-urlencode "name=Ada Lovelace" \
  --data-urlencode "note=maths & engines" \
  --data-urlencode "email=ada+test@example.com"

Multipart forms

A form with enctype="multipart/form-data", which every form with a file input must have, sends each field as a separate part divided by a boundary string. That is -F. Curl generates the boundary and writes it into the Content-Type header, which is why you must never set that header by hand on a -F request: your version would lack the boundary and the server could not split the body.

curl https://httpbin.org/post \
  -F "name=Ada Lovelace" \
  -F "avatar=@photo.png" \
  -F "bio=<bio.txt"          # < sends the file's text as a normal field

Common mistakes

  • Mixing -d and -F is an error. A request has one body and one encoding.
  • Logging in to a website this way usually also needs cookies and a CSRF token taken from the page that served the form.
  • In -F, a value starting with @ or < is read as a file. Use --form-string to send such a value literally.

Related curl examples

More under Request bodies

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I submit a form with curl?

Send each field with -d "name=value" for a normal URL-encoded form, or with -F "name=value" if the form uses multipart/form-data.

What is the difference between curl -d and -F?

-d sends application/x-www-form-urlencoded data in a single encoded string. -F sends multipart/form-data, with each field as its own part, which is required for file uploads.

How do I URL-encode form data in curl?

Use --data-urlencode "name=value" instead of -d. Curl percent-encodes the value so characters such as spaces, ampersands and plus signs arrive intact.