Understanding the Content-Type HTTP Header
Tags: webdev, programming
Published: 2021-11-10 04:18:16

The `Content-Type` header is used in web requests to indicate what type of media or resource is being used in the request or response.

When you send data in a request such as PUT or POST, you pass the `Content-Type` header to tell the server what type of data it is receiving. 

When you receive a response from a server, there will be a `Content-Type` header as well. It tells the client what type of data is being returned so it knows how to process it.

---

## Is Content-Type a required field?

Nope, Content-Type is not a required field. It's not mandatory per the HTTP 1.1 specification.

<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1" target="_blank">http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1</a>

> Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

Still, this can potentially cause problems and make it more difficult for the server to understand what it's receiving from you. To avoid any issues down the road, I suggest you pass this header along regardless.

---

## Is Content-Type case sensitive?

For your header field names, no.

If you pass `Content-Type` or `content-type` or even something very creative like `Content-TYPE`... it should work fine!

From <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2" target="_blank">http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2</a>:

> 4.2 Message Headers

> HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822 [9]. Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.

The parameter values you pass for this header can depend though.

Generally, parameter values can be case sensitive and it depends on the value of the parameter.

For example, the official <a href="https://www.w3.org/TR/html4/charset.html#h-5.2.1" target="_blank">W3C specs</a> state "Names for character encodings are case-insensitive". So, a `Content-Type: text/html; charset=UTF-8` should be equivalent to `Content-Type: text/html; charset=utf-8`.

These recommendations may also change so I advise using the proper casing from the start so you can relax and not have to worry.

---

## What is the difference between the Content-Type and Accept headers?

First, the `Accept` header is only used in requests (you probably won't see it in responses), while the `Content-Type` is used in both requests and responses.

This is because the `Accept` header is used in a request to tell the server what the type of media in the response should be. 

It's like asking the server for information and telling it what kind of information you expect back.

The `Content-Type` header tells you what the type of media in the current request actually is. 

It's like sending the server a document that asks for information, and this header tells the server what type of document you gave them so they can read it properly.

### Show me an example!

If I post an image to a website, my `Content-Type` header might be `image/jpeg` because the media in this request is a jpeg image. My `Accept` header would tell the website what kind of response I want back. If I don't care, my `Accept` header would probably be `*/*` which means anything works.

When the website gets my post request with an image, I'll get a response from them. It too will have a `Content-Type` header to tell me the type of their response. In this case, it is `application/json;charset=utf-8` meaning their server responded with JSON.

---

## What are the possible values of the Content-Type header?

Well, there's a ton. Too many to list here, so I'll cover the most common types which will probably have what you need.

If you're still curious though, here is the <a href="https://www.iana.org/assignments/media-types/media-types.xhtml" target="_blank">list of all Content-Type headers</a>.

### Text Types

- text/xml
- text/css    
- text/csv    
- text/html    
- text/plain    
- text/javascript 

### Images

- image/gif   
- image/png   
- image/jpeg   
- image/svg+xml  
- image/tiff    
- image/x-icon   
- image/vnd.djvu   

### Application Types

- application/zip   
- application/pdf    
- application/xml   
- application/ogg  
- application/json  
- application/ld+json   
- application/EDI-X12   
- application/EDIFACT  
- application/javascript   
- application/xhtml+xml   
- application/java-archive 
- application/octet-stream  
- application/x-shockwave-flash
- application/x-www-form-urlencoded  

### Audio Types

- audio/mpeg
- audio/x-wav
- audio/x-ms-wma
- audio/vnd.rn-realaudio

### Video Types

- video/quicktime
- video/webm  
- video/mpeg    
- video/mp4        
- video/x-flv       
- video/x-msvideo
- video/x-ms-wmv


### Multipart Types
These mean there are 2 or more pieces of content, each with their own type as well.

- multipart/mixed
- multipart/related
- multipart/form-data
- multipart/alternative