What is the Content-Type http header for?
Jun 12, 2013The Content-Type http request header specifies the content type of the http request payload. The Content-Type header
is NOT tied to the content type of the response sent by the server.
Here’s an example using pure JavaScript to make an asynchronous HTTP request from the browser.
var joinRequest = new XMLHttpRequest();
joinRequest.onload = function() {
if (this.status === 200) {
console.log('JOINED!', this.response); // this.response is NOT affected by 'Content-Type'
}
};
// 'Content-Type' header tells the server what type of data we are sending
joinRequest.setRequestHeader('Content-Type', 'application/json');
// 'Accept' header tells the server what content types we can accept
joinRequest.setRequestHeader('Accept', 'text/plain');
joinRequest.open('POST', '/join', true);
joinRequest.send(JSON.stringify({name:'Charles'})); Don’t confuse Content-Type with dataType, accepts, and Accept.
dataType, and accepts are jquery.ajax options.
dataTypehelps jqeury correctly process the server’s response.acceptstells jquery to include headers in the request that inform the server what type of data we are interested in.Acceptis an http request header that informs the server which data types we are interested in. The value for the Accept header must be a semicolon-separated list of MIME types.