I understand that the Accept
parameter define a data type expected in a client response sent from the server, so it's used as a response header.
My question is regarding the Content-type
, it's used by a client to define the body format of a request sent, I always used it as part of a client request, so I have a client request where I set the headers with Accept
and Content-type
. And recently, I came across a project where the Content-type
is defined in the response headers (so sent by the server). So my question is: Content-type
need to be set as part of the client request header or as part of the server response header or can it be set to both ?
TL;DR
The entity header
Content-Type
is used to indicate the media type of the resource. In responses, aContent-Type
header tells the client what the content type of the returned content actually is. In requests, such as POST or PUT, the client tells the server what type of data is actually sent.Elaborated Answer
As you correctly note, the
Accept
header is used by HTTP clients to tell the server what response media types are acceptable. The server, on their turn, will then send back a response, which will include theContent-Type
header telling the client what the media type is actually returned.Now, the
Content-Type
header could be on request and responses as well. Why? Well, think about POST or PUT requests. With those request types, the client is actually sending a bunch of data to the server as part of the request, and theContent-Type
header tells the server what the data actually is and thus determines how the server will parse it.Read the relevant RFCs. In this case 7231:
So:
Accept
indicates what kind of response from the server the client can accept.Content-type
always is about the content of the current request or response.So if your request has no payload, you don't use a content-type request header.
Accept header is used by HTTP clients to tell the server which type of content they expect/prefer as response. Content-type can be used both by clients and servers to identify the format of the data in their request (client) or response (server) and, therefore, help the other part interpret correctly the information.