Often developers get confused between Accept header and Content-type header because both are used to specify Media type.
The difference lies between entity for which they represent Media type and nature of indication.
Lets dive little in it -
- It is request type HTTP Header means only an HTTP request can have this header.
- In REST API, requests are meant to be sent from client to server. Mostly, the client is web browser.
- Accept header can specify any Media type (i.e. text/html, text/plain,text/xml, text/csv, image/png, application/json,application/xml, application/octet-stream, application/x-www-form-urlencoded etc).
- Accept Header is a way for a client to specify media-type for response-content, it is expecting.
- It basically tells server that I would like to accept this type of content.
- Accept header can have comma separated multiple values.
- Hence, it is the way server can identify which media type client is able to support and sent content accordingly during Content-Negotiation.
- If there is no accept header in request, server assumes client is able to support any media type.
- This header can be present in both Http Request and Http Response.
- HTTP Request — When this header is sent as part of request from client to server, it represents a way to specify the media type of request being sent.
- Ex — 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 the Content-Type header tells the server what the data actually is (and thus determines how the server will parse it).
- HTTP Response — When this header is sent as part of response from server to client, it specifies the media type of response content or resource.
- It is similar to Accept header of request but there is nothing like client is expecting it but server tells that response content is of specified media type.
- By Content-type, browser perform MIME Sniffing — inspect stream it received and load data accordingly.
Accept header tells server, it would like to receive data in specified media type.
Content-type actually define media type of the content(request data in case of request header and response content in case of response header)