🔗 HTTP


media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF’s RFC 6838.

The Internet Assigned Numbers Authority (IANA) is responsible for all official MIME types, and you can find the most up-to-date and complete list at their Media Types page.

Tip

Warning: Browsers use the MIME type, not the file extension, to determine how to process a URL, so it’s important that web servers send the correct MIME type in the response’s Content-Type header. If this is not correctly configured, browsers are likely to misinterpret the contents of files, sites will not work correctly, and downloaded files may be mishandled.


Structure of a MIME type

A MIME type most commonly consists of just two parts: a type and a subtype, separated by a slash (/) — with no whitespace between: type/subtype.

The type represents the general category into which the data type falls, such as video or text.

The subtype identifies the exact kind of data of the specified type the MIME type represents. For example, for the MIME type text, the subtype might be plain (plain text), html (HTMLsource code), or calendar (for iCalendar/.ics) files.

Each type has its own set of possible subtypes. A MIME type always has both a type and a subtype, never just one or the other.

An optional parameter can be added to provide additional details: type/subtype;parameter=value;  text/plain;charset=UTF-8.

MIME types are case-insensitive but are traditionally written in lowercase. The parameter values can be case-sensitive.

Discrete types

TypeDescription
applicationAny kind of binary data that doesn’t fall explicitly into one of the other types; either data that will be executed or interpreted in some way or binary data that requires a specific application or category of application to use. Generic binary data (or binary data whose true type is unknown) is application/octet-stream. Other common examples include application/pdfapplication/pkcs8, and application/zip.
audioAudio or music data. Examples include audio/mpegaudio/vorbis.
exampleReserved for use as a placeholder in examples showing how to use MIME types. These should never be used outside of sample code listings and documentation.
fontFont/typeface data. Common examples include font/wofffont/ttf, and font/otf.
imageImage or graphical data including both bitmap and vector still images as well as animated versions of still image formats such as animated GIF or APNG. Common examples are image/jpegimage/png, and image/svg+xml.
modelModel data for a 3D object or scene. Examples include model/3mf and model/vrml.
textText-only data including any human-readable content, source code, or textual data such as comma-separated value (CSV) formatted data. Examples include: text/plaintext/csv, and text/html.
videoVideo data or files, such as MP4 movies (video/mp4).
For text documents without a specific subtype, text/plain should be used. Similarly, for binary documents without a specific or known subtype, application/octet-stream should be used.

Multipart types

Multipart types indicate a category of document broken into pieces, often with different MIME types; they can also be used — especially in email scenarios — to represent multiple, separate files which are all part of the same transaction. They represent a composite document.

Except for multipart/form-data, used in the POST method of HTML Forms, and multipart/byteranges, used with 206 Partial Content to send part of a document, HTTP doesn’t handle multipart documents in a special way: the message is transmitted to the browser (which will likely show a “Save As” window if it doesn’t know how to display the document).

TypeDescription
messageA message that encapsulates other messages. This can be used, for instance, to represent an email that includes a forwarded message as part of its data, or to allow sending very large messages in chunks as if it were multiple messages.
multipartData that consists of multiple components which may individually have different MIME types. Examples include multipart/form-data (for data produced using the FormData API) and multipart/byteranges (defined in RFC 7233, section 5.4.1 and used with HTTP’s 206“Partial Content” response returned when the fetched data is only part of the content, such as is delivered using the Range header).

multipart/form-data

The multipart/form-data type can be used when sending the values of a completed HTML Form from browser to server.

As a multipart document format, it consists of different parts, delimited by a boundary (a string starting with a double dash --). Each part is its own entity with its own HTTP headers, Content-Disposition, and Content-Type for file uploading fields.

Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)

--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg

(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"

(data)
--aBoundaryString
(more subparts)
--aBoundaryString--

The following <form>:

<form
  action="http://localhost:8000/"
  method="post"
  enctype="multipart/form-data">
  <label>Name: <input name="myTextField" value="Test" /></label>
  <label><input type="checkbox" name="myCheckBox" /> Check</label>
  <label>
    Upload file: <input type="file" name="myFile" value="test.txt" />
  </label>
  <button>Send the file</button>
</form>

will send this message:

POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498
Content-Length: 465
 
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myTextField"
 
Test
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myCheckBox"
 
on
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain
 
Simple file.
-----------------------------8721656041911415653955004498--

multipart/byteranges

The multipart/byteranges MIME type is used to send partial responses to the browser.

When the 206 Partial Content status code is sent, this MIME type indicates that the document is composed of several parts, one for each of the requested ranges. Like other multipart types, the Content-Type uses a boundary to separate the pieces. Each piece has a Content-Typeheader with its actual type and a Content-Range of the range it represents.

HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 385
 
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-200/1270
 
eta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 300-400/1270
 
-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica
--3d6b6a416f9b5--

References


📂 HTTP | Последнее изменение: 20.02.2024 14:49