When APP was not popular in the early years, PHP used to develop website form submission and obtain post parameters using the $_POST object. Later, I started to develop the app interface and communicated with front-end colleagues using json format data transfer. The parameters passed in the post request were also in json format. I didn't quite understand it at first, thinking that the body in the http request header was in this format:
json_param=A string in json format
It is to seal all the parameters together, then encode it into json format, and finally pass it up in the form of kv, but later found that it is not. The so-called json data format is that the body in the http request is a string in json format , this uses $_POST You can't get it. The following combines the http header information to deal with the differences in the http request body of these methods and how to obtain this type of parameter.
In the early years of web form submission, $_POST was used to obtain request parameters. In fact, they existed in the form of kv values in the http request header, such as:
Web form submission http request header
When php receives this kind of request, the underlying system of php will parse and store this string in the $_POST variable, so these parameters can be obtained through $_POST in php. Note that the Content-Type here is: application/x-www-form-urlencoded .
Of course, when uploading files, the form needs to add an enctype=”multipart/form-data”. I didn't understand why in the early years, but later I learned that the http header format has changed at this time, as shown in the following figure:
Upload file http request header
Content-Type has become a multipart/form-data format . The data acquisition in this format is also processed at the bottom of php. Ordinary non-file parameters can be obtained through $_POST, and file parameters can be obtained through $_FILES.
So what is the json format data mentioned at the beginning? Let's analyze it below.
At first I tried to get it with $_POST, but the result was not obtained. Later, I learned after capturing the package: The json format data refers to the http header body string is a json format string
The following interception of the http header will understand.
http request parameters are in json format
This is not available through $_POST in PHP, and the underlying PHP layer does not handle this method. So how can you get it? It can be obtained in php as follows:
Seeing this, it suddenly dawned on me that the byte stream in the body was also obtained in this way when processing flash upload pictures. After getting the data in the body, the subsequent processing is simple, just json_decode directly.
After stepping through the pit, I learned a lot of new things. The http request header actually contains a lot of formats, in addition to json, xml, html, and so on.
http various request headers
Why use json to transfer data instead of the traditional kv format? The practicality of this method also has an advantage, that is, it can well support the transmission of multi-dimensional arrays. For example, when placing an order, there will be more than one commodity parameters passed. The traditional kv form is not easy to handle, and the json method is used. It's very easy to solve.