Layout |
title |
Date |
Comments |
Categories |
Post |
MIME Types in Rails |
2014-09-08 21:40 |
True |
Ruby |
In rails development, different request formats are often used to handle different responses, the most common being the same action that responds differently to the Html/text and JSON formats. So what are the response formats in rails that already handle these request formats? In this article, we'll talk about how to handle rails.
What is MIME?
First look at the standard definition of MIME. MIME (Multipurpose Internet Mail Extensions) multi-purpose Internet Mail extension type, is the type of file that is used to set an extension for an application to open, when the extension file is accessed, the browser automatically Opens by using the specified application.
MIME was earlier applied to the e-mail system and later to the browser. The browser will open different files, such as the MP3 file, based on the MIME type. In the early HTTP protocol there is no additional data type information, all transmitted data is interpreted as Hypertext Markup Language (HTML document), when MIME is supported after HTTP transmission is not only ordinary text, but we now see a variety of data forms.
The definition and composition of MIME
The mine type is made up of two parts, preceded by a large category of data, text, image, and so on, which define the specific species later. For example:
//大类别为text html文本 .html text/html xml文档 .xml text/xml 普通文本 .txt text/plain//大类别为application XHTML文档 .xhtml application/xhtml+xml pdf文档 .pdf application/pdf//大类别为image png图像 .png image/png git图形 .gif image/gif
MIME is actually a specialized organization IANA to confirm the standard MIME type, but because the internet is growing too fast, the IANA to determine the standard is far behind the speed of application development. As a result, most now use the fact standard, which is the MIME type recognized by both the server and the browser, and usually the Web server (the MIME type in rails below) and the browser have set the common MIME type by default, when you really need to use some unusual MIME types, Can be set at the same time on the server and browser, so that the browser resolves.
On the server side, different MIME types are identified by the requested suffix name, so the server must have a corresponding relationship between the request suffix and the MIME type defined (as defined in rails in action_dispatch/http/mime_types.rb). When the browser accepts the response data, the data stream of the server is accepted, that is, the name of the file is not parsed, and to allow the browser to recognize different types of file data, MIME type information needs to be set in the response data. Before the server responds to the data, the first thing is to set the MIME type information of the data, which is set by the Content-type keyword of the HTTP information header.
//冒号之后必须一个空格Content-type: text/html
MIME Types in Rails
Next, let's look at how MIME types are handled in rails. As we've said above, the server is using the request suffix to identify the MIME type. The request suffix of rails can be obtained by Request.formart, specifically looking at the processing details of format:
def formats @env["action_dispatch.request.formats"] ||= if parameters[:format] Array(Mime[parameters[:format]]) elsif use_accept_header && valid_accept_header accepts elsif xhr? [Mime::JS] else [Mime::HTML] endend
As you can see from the code above, rails will first accept our own set of format parameters, which can be set in 3 ways:
1. url suffix, such as: Users.json, Users/1.json
2. Pass through the Foramt parameter, such as: {id:1, Format: ' JSON '}
3. Set the default Formart in the route, such as: defaults: {format:: JSON}
If you do not specify the format parameter, rails checks your request for the header, which is what the accepts method does, which is actually looking at Content-type.
The MIME type is not found above and is an asynchronous request, set format to ' JS ' type directly (note, not JSON).
The last is not found, according to ordinary HTML processing, that is, ' text/html '.
As mentioned earlier, the server maintains a correspondence between the format and the mine type, which is handled in rails by Mime::type, calling Mime::type.register to register the new MIME type, as shown in action_dispatch/ HTTP/MIME_TYPES.RB file.
Do not use JSON type to determine whether it is an asynchronous request
When I first started to learn JavaScript, it was easy to make a mistake by using the JSON request suffix to determine if it was an AJAX request, which is mostly no problem, because it is almost customary to set the AJAX request as a JSON suffix. In fact, this is not true, the above code can be seen to judge the asynchronous request rails is using the Xhr method, this method is done:
def xml_http_request? @env[‘HTTP_X_REQUESTED_WITH‘] =~ /XMLHttpRequest/iendalias :xhr? :xml_http_request?
It is clear that the ' xmlhttprequest ' keyword of the HTTP request is used to determine whether it is asynchronous, not the content-type of the requested header.
In summary, this article describes the definition of MIME types, components and functions, as well as the processing of MIME classes in rails, I hope you have some understanding.
Reference Link: Https://github.com/nateyu/nateyu.github.com/blob/master/_posts/2014-09-08-railsde-zhong-de-mime-type.markdown
MIME Types in Rails