Abstract: When a 400 error occurs in an http request, it is usually caused by an invalid http request (bad request) and incorrect url parameter encoding format. The URL has certain format requirements. Generally, only English letters, Arabic numerals and some special characters can be used. Other characters such as spaces and double quotes need to be encoded before the URL can be used. 1. When accessing directly in the browser, the browser will automatically perform encoding processing. 2. However, when calling directly in the program, the URL needs to be encoded in advance. For example, when PHP is called using curl_exec, illegal characters such as spaces must be encoded. 3. In particular, you need to pay more attention to the json data value contained in the url parameter.
Background
Phenomena 1 - The php encapsulated http request interface calls the url
1. The rawheader value of response shows a 400 error
1 |
|
2. Self-encapsulated sending http request interface
1 2 3 4 5 6 7 8 9 10 11 12 |
|
3.Request The send method of the Request object encapsulated in .php
The Request::send method in nategood/httpful uses the curl_exec interface of PHP to initiate an http request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
4. The url value before curl_exec is called
1 |
|
Phenomenon 2 - Directly accessing the url in the browser
1 |
|
Cause analysis
Comparing the url values in Phenomenon 1 and Phenomenon 2, we can find:
fileIndex parameter The value is a json string
The space character in the title attribute value in the fileIndex parameter value is encoded as %20 in phenomenon 2, and the double quotation mark " is encoded as %22. This operation is automatically performed by the browser
Replace the url After the spaces in are encoded, the curl_exec interface returns 200
Test code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Percent encoding (that is, url encoding)
Generally speaking, URLs can only use English letters, Arabic numerals and certain punctuation marks, and cannot be used Other words and symbols. Ruan Yifeng mentioned in his blog post that the network standard RFC 1738 has a mandatory requirement:
"...Only alphanumerics [0-9a-zA-Z], the special characters "$-.+ !*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL."
"Only letters and numbers [0-9a-zA-Z], some The special symbols "$-.+!*'()," [excluding double quotes], and certain reserved words can be used directly in URLs without encoding. "
url encoding is the encoding mechanism of the Uniform Resource Locator (URL) in a specific context. If the server encounters non-standard characters when parsing the http request, it will encode the non-standard characters.
Summary
When the http request appears with 400 When the error occurs, it is usually caused by an invalid http request (bad request) and incorrect URL parameter encoding format. URLs have certain format requirements. Generally, only English letters, Arabic numerals and some special characters can be used. Other characters such as spaces and double characters can be used. Quotation marks need to be encoded before the user URL can be used.
The browser will automatically encode it when accessed directly in the browser. However, when calling directly in the program, the url needs to be encoded in advance. For example, when calling PHP using curl_exec, it will be encoded. Encoding illegal characters such as spaces
Especially when url parameters contain json data values
.