Skip to content

Instantly share code, notes, and snippets.

Last active June 8, 2023 07:45
Star You must be signed in to star a gist
What would you like to do?
form-data vs -urlencoded

Nice answer on stackoverflow to the question of when to use one or the other content-types for POSTing data, viz. application/x-www-form-urlencoded and multipart/form-data.

“The moral of the story is, if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data. Otherwise, use application/x-www-form-urlencoded.”

Matt Bridges' answer in full:

The MIME types you mention are the two Content-Type headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.

For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equal symbal (=). An example of this would be:


According to the specification:

[Reserved and] non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character

That means that for each non-alphanumeric byte that exists in one of our values, it's going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.

That's where multipart/form-data comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type, and particularly Content-Disposition, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).

Why not use multipart/form-data all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.

The moral of the story is, if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data. Otherwise, use application/x-www-form-urlencoded.

POST examples with curl

For details and more examples, see the POST section of the official tutorial.

In the examples below, suppose we need to POST data to, the canonical address for the shoes resource (a "collection" resource, in REST-speak) of a particular user (joyrexus):



curl -d "brand=nike" -d "color=red" -d "size=11" $URL

curl --data "brand=nike&color=red&size=11" $URL


curl --form "image=@nikes.png" --form "brand=nike" --form "color=red" --form "size=11" $URL

curl -F "image=@nikes.png" -F "brand=nike" -F "color=red" -F "size=11" $URL

Change the name field of a file upload part by setting filename=:

curl -F "image=@nikes.png;filename={desired-name}.png" -F "brand=nike" -F "color=red" -F "size=11" $URL

Specify Content-Type by using type=:

curl -F "image=@nikes.png;filename={desired-name}.png;type=image/png" -F "brand=nike" -F "color=red" -F "size=11" $URL

Sans data

curl --data '' $URL

curl -X POST $URL

curl --request POST $URL
Copy link

mboo2005 commented Jan 4, 2016


Copy link

redochka commented Apr 8, 2016

Thank you!

Copy link

Great sample and explanation.

Copy link

Nice explanation!

Copy link

Nice explanation.Great !!!

Copy link

Nice! Tanks!

Copy link

Great explanation with examples

Copy link

joyrexus commented Sep 5, 2017

Copy link

Please help me to encode filename of request. following request suite for only english chars:
curl -X POST
-H 'content-type: multipart/form-data; charset=utf-8; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
-F data='@/home/user/Downloads/tmp/Бамбук.xls'

Copy link

please explain. what is mean by "application/x-www-form-urlencoded"?

Copy link

nice explanation

Copy link

Heunsig commented Aug 17, 2018


Copy link

I just got a response from an API based in perl and it is non-urlencoded and is separated by line.... when is this acceptable? I say NEVER!

Copy link

Many thanks. Really useful.

It would be great to do the same using Python requests library. Need to force multipart/form-data witohout sending a file using requests.

Copy link


Copy link

Perfect. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment