Using MIME Type Header [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. In this PHP MIME Type Header example, we are sending a request to ReqBin echo URL with Content-Type HTTP header. Click Click Send to run PHP MIME Type Header example online and see results. The PHP code was automatically generated for the Mime Type example.
Using MIME Type Header [PHP Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 79

{
  "Id": 1890,
  "Customer": "Eric Sweet",
  "Quantity": 1,
  "Price": 19.00
}
Updated: Viewed: 53803 times
PHP code for Mime Type example

PHP code for Mime Type Example

This PHP code snippet was generated automatically for the Mime Type example.
<< Back to the Mime Type example

What is MIME Type?

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.

What is the MIME structure?

MIME types usually consist of three parts: type, subtype, and optional parameters, separated by a forward slash "/" without spaces. The MIME type is not case-sensitive but is usually written in lowercase letters. A MIME type must have a type as well as a subtype. Each type has its own set of possible subtypes. For example, text/plain; charset=UTF-8 describes data in plain text format with UTF-8 encoding. The following is an example of the MIME type of an HTTP request:

HTTP Request MIME Type Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 81

{...}

The following is an example of the MIME type of an HTTP response

HTTP Response MIME Type Example
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

<html>...</html>

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.

Multipart Message Example
POST echo/post/form HTTP/1.1
Host: reqbin.com
Content-Type: multipart/form-data; boundary=-------10591404156
Content-Length: 554

---------10591404156
Content-Disposition: form-data; name="text"

some text
---------10591404156
Content-Disposition: form-data; name="file1"; filename="readme.txt"
Content-Type: text/plain

[readme.txt file contents]

---------10591404156
Content-Disposition: form-data; name="file2"; filename="image.png"
Content-Type: image/png

[image.png file contents]

---------10591404156--

Common MIME Types

The IANA is the official registry of MIME media types and maintains a list of all known official MIME types. Below is a short list of popular MIME types for the Internet.

  • Images: image/jpeg, image/png, image/gif, image/svg+xml
  • Files: text/plain, application/pdf, application/zip
  • Applications: application/octet-stream, application/pkcs8
  • Media: audio/mpeg, video/mp4, video/mpeg
  • Data: application/json, application/xml

See also

Generate code snippets for PHP and other programming languages

Convert your Mime Type request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.