For application / x-www-form-urlencoded, spaces must be replaced with "+", so you may need an encodeURIComponent with the additional replacement of "% 20" with "+".

Source: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

  • Because "...- urlencoded", that is, "encoded as in the URL." - ߊߚߤߘ
  • @Arhad In the URL “% 20” is valid, but in application / x-www-form-urlencoded it is not. I ask why it is not permissible. These are different things. - user208916

2 answers 2

Go to the wiki . For the application/x-www-form-urlencoded data type, there is a link to the HTML 4.01 Specification, Section 17.13.4.1 document , which says the following:

application / x-www-form-urlencoded

This is the default content type. Forms must be encoded as follows:

  1. Control names and values ​​are escaped. The characters have been replaced by the '+' , and then the reserved characters are escaped as described in [RFC1738], section 2.2: ASCII code of the character. Line breaks are not represented as "CR LF" pairs (ie, '% 0D% 0A').
  2. The control names / values ​​are listed in the order. The name is separated from the value by '=' and the name / value pairs are separated from each other by `& '.
  • Is there any explanation why space characters are replaced exactly by '+'? Or "historically" ™? - andreymal
  • Add. question: encodeURIComponent(search).replace(/%20/g, "+"); Is it safe to use? Doesn't "% 20" match the coding sequence that encodes some other character? - user208916
  • Perhaps there is an explanation for just such a solution for the specification, but I do not know it. %20 can not match anything except a space. Neither in single-byte encodings, nor in multibyte. In the same UTF-8, the characters represented by several bytes should not contain bytes with a value from 00 to 7F . - Visman
  • @Visman and if En Quad in UTF-16BE? :) - andreymal
  • @andreymal, and UTF-16 and UTF-32 used in the web? - Visman

For MIME application/x-www-form-urlencoded , the same coding scheme is used as for the URL (for “...- urlencoded”, that is, “encoded as in the URL”).

In turn, in the URL encoding space plus is an agreement. The reason for this agreement is simple - readability. The fact is that the web was created in English-speaking USA, and they didn’t even think about using non-Latin addresses (or thought, but decided that there would be very few of them), and therefore each letter was encoded as is, without escape sequences.

Compare any:

www.example.org/fileName.asp?param1=some+Param+with+Long+length&bar=5000

and:

www.example.org/fileName.asp?param1=some%20Param%20with%20Long%20length&bar=5000

I think that it is not only for me to read the first version easier and faster, especially in the case of words beginning with capital letters.

P. S: In the case of using non-Latin characters, when the entire line consists of only escape sequences, there is no difference in the ways of writing spaces, yes.