Content-Type or MIME type in Post
The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending).
Syntax
Most used
The most commonly used Content-Type
(or MIME type) for POST HTTP requests depends on the type of data being sent. Here are a few of the most frequently used Content-Type headers:
- application/json: Used for sending JSON data.
Examples
Content-Type in HTML forms
In a POST request, resulting from an HTML form submission, the Content-Type of the request is specified by the enctype attribute on the <form>
element.
<form action="/foo" method="post" enctype="multipart/form-data">
<input type="text" name="description" value="some text" />
<input type="file" name="myFile" />
<button type="submit">Submit</button>
</form>
The request looks something like this (less interesting headers are omitted here):
POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575
-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="description"
some text
-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="myFile"; filename="foo.txt"
Content-Type: text/plain
(content of the uploaded file foo.txt)
-----------------------------974767299852498929531610575--
MIME types
A MIME type (now properly called "media type", but also sometimes "content type") is a string sent along with a file indicating the type of the file (describing the content format, for example, a sound file might be labeled audio/ogg
, or an image file image/png
).
Multipart types
There are two multipart types:
- message
- multipart
Important MIME types for Web developers
-
application/octet-stream
This is the default for
binary files
. As it means unknown binary file, browsers usually don't execute it, or even ask if it should be executed. They treat it as if the Content-Disposition header was set toattachment
, and propose a "Save As" dialog. -
text/plain
This is the default for textual files. Even if it really means "unknown textual file," browsers assume they can display it.
Example
Application
application/json: JSON data.
Content-Type: application/json
application/xml: XML data.
Content-Type: application/xml
application/octet-stream: Arbitrary binary data. Often used for file downloads.
Content-Type: application/octet-stream
application/pdf: PDF documents.
Content-Type: application/pdf
application/x-www-form-urlencoded: Form data in URL-encoded format.
Content-Type: application/x-www-form-urlencoded
multipart/form-data: Form data that includes files or binary data.
Content-Type: multipart/form-data; boundary=---boundary
Understanding these MIME types is crucial for web development as they help ensure that the server and browser correctly interpret and handle various types of content. Using the correct MIME type ensures proper rendering and functionality of web pages and applications.
MIME-Type & Content-Type Realtionship
MIME Type
- What it is: A way to describe the type of content.
- Format:
type/subtype
(e.g.,application/json
,text/html
). - Example:
application/json
means the content is JSON.
Content-Type
- What it is: An HTTP header that tells the server what type of content is being sent.
- What it contains: A MIME type.
- Example:
Content-Type: application/json
means the content being sent is JSON.
Relationship
- The
Content-Type
header uses MIME types to specify the format of the data. - In other words: MIME type is the value, and
Content-Type
is the header that holds that value.
Example in a POST Request
When sending JSON data in a POST request:
POST /api/resource HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 27
{
"name": "John",
"age": 30
}
- Content-Type header:
Content-Type: application/json
- MIME type used:
application/json
Summary
- MIME Type: Describes the type of content (e.g.,
application/json
). - Content-Type: The HTTP header that includes the MIME type to tell the server what kind of data you're sending.
So, MIME type and Content-Type are closely related, with MIME type being the description of the content and Content-Type being the header that communicates this description in HTTP requests.