What is the MIME type and how do I use it? [PHP Code]
MIME (Multipurpose Internet Mail Extensions) type is a standard way of describing a data type in the body of an HTTP message or email. The MIME type is passed in the Content-Type header. For example, the Content-Type: text/html header tells the browser that it received an HTML page. Based on this MIME-type header, the browser can parse and display the received HTML page correctly. Click Send to post a test request to the server and see the server's response MIME type. The PHP code was automatically generated for the Mime Type example.
MIME stands for Multipurpose Internet Mail Extensions.It is a fundamental part of communication protocols such as HTTP. The MIME type is required when you need to transfer non-text data. MIME was originally designed to extend the capabilities of email to support more data types such as non-ASCII text files and binary files such as images, PDFs, and executables. The use of MIME is not limited to email. It is now actively used in all communications over the Internet to describe the type of data sent to or from a server. It is often referred to as a media type or MIME content type. In HTTP, the MIME-type is part of the Content-Type header of an HTTP message and defines the data type in the body of an HTTP request or response. Setting the correct MIME data type in the body of the message is critical for both HTTP requests and responses and allows you to control how the request is interpreted by the client and server. Web servers and browsers have a list of well-known file extensions and MIME types. This helps them identify and interpret all known file types, regardless of the operating system and hardware used by the user.
MIME types usually have three parts: type, subtype, and additional media type parameters. For example, text/plain; charset=UTF-8 describes data in plain text format with UTF-8 encoding.
HTTP Request MIME Type Example
POST /echo/post/json HTTP/1.1
HTTP Response MIME Type Example
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
How to pass multiple objects in a single MIME message?
The MIME format supports the transmission of multiple objects in one message. Entities can be transferred not only in the form of a single-level sequence but also in the form of a hierarchy with the nesting of elements into each other. The multipart/* media type is used to indicate multiple contents. To transmit a multipart message, a boundary parameter is added to the Content-Type header, which denotes the sequence of characters that separate the parts of the message. The border can consist of numbers, letters, and symbols () + _, -. /: = ?.
The beginning of each part of the message is indicated by the string --boundary. The end of the last message is indicated by the string --boundary--. At the very beginning of the included part, there are headers that describe its content (Content-Type, Content-Length, etc.). If Content-Type is not specified, then text/plain is used by default.