Content-Type type in http

Browser Content-Type

Recently, when downloading on the web side, I need to return a binary stream to the front end, and I need to set one in the request header.

writer.Header().Set("Content-Type", "application/octet-stream")

So what are the specific Content-Types in http? What are their specific usage scenarios?

1 Category

1. MediaType class

MediaType is Internet Media Type, Internet media type; also called MIME type. In the HTTP protocol message header, Content-Type is used to represent the media type information in the specific request. To be precise, the client tells the server that it The type of data structure carried in the request message to be sent so that the server can process it in an appropriate manner after receiving it.

  • Official website: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

1.1 text/html: HTML format

1.2 text/plain: plain text format

1.3 text/xml: XML format

1.4 image/gif: gif image format

1.5 image/jpeg: jpg image format

1.6 image/png: png image format

2. Application class

2.1 application/xhtml+xml: XHTML format

2.2 application/xml: xml data format

2.3 application/atom+xml: Atom XML aggregation format

2.4 application/json: JSON data format

2.5 application/pdf: pdf format

2.6 application/msword: word document format

2.7 application/octet-stream: binary stream data (commonly used in file downloads)

2.8 application/x-www-form-urlencoded: form submission in html

The default encType in <form encType=””>, the form form data is encoded into key/value format and sent to the server (the default format for submitting data in the form)

2.9 multipart/form-data: When file upload is required in the form

multipart/form-data: This format needs to be used when uploading files in a form.

2.10 application/zip: compressed package

2 The role of Content-Type (generally not required for GET requests)

However, it should be noted that generally get requests do not need to set Content-Type, only posts need to set it!
Why does the get request not need to set Content-Type?

2.1 Concept

The role of Content-Type is: to tell others what type of data I carry

  • For request request

Get does not carry data, and the parameters after ? in the URL are not counted as data
Post needs to carry parameters, that is, data parameters. The client tells the server that it data type

  • For response

On the other hand, the server tells the client its own data type, so that the browser knows whether to render the page as text/html or as text/plain.

2.2 Case

We simply write an html page, then set different Content-Types respectively, and observe the results.

①Set to text/html

When the original text is returned, set Content-Type in responseHeaders, and its value is 'text/html':

response.writeHead(200, {
    
    'Content-Type': 'text/html'});    

The access effect is:
Insert image description here
The browser renders the text as html, then the html tag will be hidden and rendered according to css styles!

②Set to text/plain

Now let's try changing the Content-Type to text/plain:

response.writeHead(200, {
    
    'Content-Type': 'text/plain'});    

The access effect is:
Insert image description here
Note that the content marked in red is an html tag and is displayed as ordinary text! Because the server tells the browser that the data is in text format, not html format, the browser treats the tag as ordinary text.

Therefore, the role of Content-Typ is to tell others what format my data is in. The client can tell the server, or the server can tell the client.

Back to the previous question, why does the get request not need to set Content-Type?
The reason is that when getting, it will not carry narrowly defined data, that is, data, so naturally there is no need to tell the server what its data type is! Of course, it won't go wrong if you forcibly set the Content-Type for the get request, but it is meaningless

expand

1. Content-Type and Content-Disposition relationship

  1. The Content-Type entity header is used to indicate the MIME type of the resource. The Content-Type header tells the client how the actual returned content should display the data returned by the response.
  2. Content-Disposition is an extension of the MIME protocol that indicates how the reply content should be displayed.
  • In inline form: that is: a web page or part of a page, this method has the same effect as Content-Type
  • As attachment: Download as attachment and save to local
//在页面中展示(作为页面的一部分)
Content-Disposition: inline
//attachment意为消息体应该被下载到本地,大多数浏览器会呈现一个"保存为对话框"
Content-Disposition: attachment
//将response结果下载到本地,并且文件名为xxx.jpg
Content-Disposition: attachment; filename="xxx.jpg"

Reference article: https://blog.csdn.net/m0_45406092/article/details/114022550

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/134222714