Home >Web Front-end >JS Tutorial >http format when requesting to upload files and text in post
The server usually knows the method of the message body in the request based on the Content-Type field in the request headers* Encode*, then parse the body. So when it comes to POST submission data scheme, it includes two parts: Content-Type and message body encoding method.
The most basic form form structure, key-value pair used to pass character parameters, The request structure is as follows
POST HTTP/1.1Host: www.demo.comCache-Control: no-cachePostman-Token: 81d7b315-d4be-8ee8-1237-04f3976de032Content-Type: application/x-www-form-urlencodedkey=value&testKey=testValue
The Content-Type in the request header is set to application/x-www-form-urlencoded; the submitted data is encoded in the request body according to key1=value1&key2=value2, and both key and value are To performurlEncode;
This is the most common data submission method when uploading files. Take a look at the request structure
POST HTTP/1.1Host: www.demo.comCache-Control: no-cachePostman-Token: 679d816d-8757-14fd-57f2-fbc2518dddd9Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="key"value------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="testKey"testValue------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="imgFile"; filename="no-file"Content-Type: application/octet-stream<data in here>------WebKitFormBoundary7MA4YWxkTrZu0gW--
First of all, Content-Type in the request header is multipart/form-data; and a boundary will randomly be generated to distinguish each data in the request body. ; Each data starts with –boundary, followed by a line break, followed by content description information, followed by 2 lines, followed by data; and then ends with –boundary–, and finally a line break;
The content descriptions of text data and files and pictures are different
Text parameters:
Content-Disposition: form-data; name="key"Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 8bit
File parameters:
Content-Disposition: form-data; name="imgFile"; filename="no-file"Content-Type: application/octet-streamContent-Transfer-Encoding: binary
Each newline is \r\ n ;
application/json
text/xml
text/plain
It is also very common to set the Content-Type of the request header to these, However, generally in web front-end development, the request body does not have a fixed structure, and the data stream of the corresponding data is directly transmitted. It does not have to be the same as the above two methods, and it must be wrapped in a fixed structure, but the data corresponds to json, xml, and text. ;
The above is the detailed content of http format when requesting to upload files and text in post. For more information, please follow other related articles on the PHP Chinese website!