What is the difference between x-www-form-urlencoded and form-data? [duplicate]

5

Is there any relevant difference between content-type x-www-form-urlencoded and form-data ?

I always have doubts when I should use one or the other because I do not know if there are any shocking differences.

    
asked by anonymous 10.12.2015 / 17:43

1 answer

9

This content-type specifies how the form data should be encoded when sent to the server (only when method="post")

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

Name = rafael & city = campinas & age = 31

This means that for every non-alphanumeric byte that exists in one of our values, it will take three bytes to represent it. For large binary files, tripling the load will be highly inefficient.

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

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

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

source: link

    
10.12.2015 / 18:03