About the content-type

Content-type is the MIME Type, or resource, used by the entity header field (or entity header) to indicate to the receiver the media Type of the entity body. Media Type is more commonly used today. (For example, specify the type of entity media that the HEAD method sends to the recipient, or the type of request media that the GET method sends to indicate what MIME type the following document belongs to.)

In the response, the Content-Type header tells the client the Content Type of the actual returned Content. The browser does MIME sniffing in some cases and does not necessarily follow the value of the header; To prevent this behavior, set the title X-Content-type-options to nosniff.

In a request (such as POST or PUT), the client tells the server what type of data is actually being sent.

grammar

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something[1 to 70 characters]
Copy the code

Media-type Media type of the resource or data

Charset character encoding standard

Boundary Is required for multipart entities to encapsulate the boundaries of multiple parts of a message. It consists of 1 to 70 characters, which are automatically generated by the browser. The character set is robust to passing through the gateway and does not end with a blank space.

example

In a POST request generated through an HTML form submission, the content-type of the request header is specified by the encType attribute on the element:

<form action="/" method="post" enctype="multipart/form-data">
  <input type="text" name="description" value="some text">
  <input type="file" name="myFile">
  <button type="submit">Submit</button>
</form>
Copy the code

POST Common data submission types

The most common methods of submitting data in HTTP are GET and POST. The GET method, is the key value of the parameter at the rear of the QueryString ways in the URL, for example: www.example.com/test.html?a… The POST method, which usually places the Form to be submitted in a Form, specifies the action to submit the data

Submitted data is encoded according to content Type through the form encType attribute, which specifies how form data should be encoded before it is sent to the server. And, if GET, use “? Connect, coded as “Application/X-www-form-urlencoded”; If it is POST, the content-type is determined according to the encType attribute, which is also “Application/X-www-form-urlencoded” by default.

application/x-www-form-urlencoded

The most common way to submit data is by POST, native Form Form. If the encType attribute is not set, the default is Application/X-www-form-urlencoded

POST http://www.example.com HTTP/1.1 Content-Type: Application/X-www-form-urlencoded; charset=utf-8 name=test&val1=1&val2=%E6%B5%8B%E8%AF%95&val3%5B%5D=2Copy the code

First, content-Type is specified as Application/X-www-form-urlencoded; Second, the submitted form data is converted to key-value pairs and encoded as key1=val1&key2=val2, with both key and VAL URL transcoding. Most server-side languages have good support for this approach.

You can also use this approach when submitting data using AJAX. For example, jQuery, content-type defaults to “Application/X-www-form-urlencoded; Charset = utf-8 “.

multipart/form-data

Another common way to submit POST data, the encType of the Form is set to multipart/form-data, which processes the Form’s data into a message, grouped by label and separated by a delimiter (this is what boundary does). Similar to our content-Type example above.

Since this way the data has many parts, it can upload both key-value pairs and files, or even multiple files. When the uploaded field is a file, the content-Type is used to specify the file Type. Content-disposition, which describes some information about the field. Each section starts with — boundary, followed by the content description information, followed by carriage return, and finally the specific content of the field (field, text or binary, etc.). If you are transferring a file, also include the filename and file type information. The message body ends with a — boundary — identifier.

application/json

Content-type: application/json is a common response header. In fact, it is increasingly used as a request header to tell the server that the body of the message is a serialized JSON string, one of the benefits of which is that the JSON format supports structured data that is much more complex than key-value pairs. Due to the popularity of the JSON specification, json.stringify is natively supported in every major browser except for earlier versions of IE, and server-side languages have functions that handle JSON without difficulty.

The Ajax functionality in Google’s AngularJS, by default, is to submit a JSON string.

text/xml

XML role is self-evident, used to transport and store data, it is very suitable for the world wide web transmission, provides a unified method to describe and exchange structured data, independent of the application or suppliers in JSON before there is a big industry standards (and is, of course), compared the advantages and disadvantages of JSON everyone interested can search on the Internet. Therefore, the XML type is also indispensable when submitting data to POST, although JSON may be lighter and more flexible in general scenarios.

binary (application/octet-stream)

In Chrome’s Postman tool, you can also see the type “binary”, which refers to some type of binary file. For example, application/ PDF, specifies the MIME type of a particular binary file. For text file types, if there is no specific subtype, text/plain is used. Similarly, binaries have no specific or known subtype, that is, application/octet-stream is used, which is the default value for application files and is rarely used directly.

For application/ OCtet-stream, only binaries can be committed, and only one binary can be committed, and only one file, if any, can be committed, and only one background receiver can be a stream (or byte array).

Many Web servers use the default application/ OCtet-stream to send unknown types. For security reasons, the browser does not allow custom default actions for these resources, so users must store them locally for use. In general, it is important to set the correct MIME type.

Source link: blog.csdn.net/woaixiaoyu5…