Content-Type vs Accept HTTP Headers — Request and Response Format
Fast recall
Quick revision
Core answer
Understanding of HTTP content negotiation mechanics and request/response header directionality
Confusing Content-Type as controlling response format or thinking Accept applies to the request body
Explain the bidirectional nature: Content-Type flows with the body (request or response), Accept is a client preference declaration
Interview-ready guide
Interview answer
For example, POST /orders with Content-Type: application/json and Accept: application/json says the request body is JSON and a JSON response is acceptable. If the endpoint consumes only JSON but receives text/plain, it can return 415. If it can write only JSON while the client accepts only application/xml, it can return 406.
Content-Type vs Accept HTTP Headers — Request and Response Format
Direct Answer
Content-Type describes the representation carried by the current message body. Accept lists response media types the client can accept. A request with an unsupported body Content-Type can produce 415, while an endpoint unable to produce any acceptable response type can produce 406. Not every request has a body, so Content-Type is not required on every call.
Code Example
var restTemplate = new RestTemplate();
var headers = new HttpHeaders();
// Content-Type: what format am I sending?
headers.setContentType(MediaType.APPLICATION_JSON);
// Accept: what format do I want back?
headers.setAccept(List.of(MediaType.APPLICATION_XML));
var order = new Order(null, "Laptop", 1299.99);
var request = new HttpEntity<>(order, headers);
// Sends JSON (Content-Type), expects XML back (Accept)
var response = restTemplate.postForEntity(
"http://localhost:8080/api/orders",
request,
Order.class
);
// Server sees Content-Type: application/json -> deserializes request as JSON
// Server sees Accept: application/xml -> serializes response as XML
Common Mistakes & Pitfalls
- Using Content-Type to request response format: Freshers often think setting Content-Type: application/xml will make the server return XML. Content-Type only describes what you're sending, not what you want back—that's Accept's job.
- Forgetting Content-Type on POST requests: Sending JSON data without Content-Type: application/json causes 415 errors because Spring doesn't know how to parse the body. The header isn't optional when there's a body.
- Expecting Accept to affect request parsing: Setting Accept: application/xml doesn't change how Spring reads your request body—it only affects the response. If you send JSON, you still need Content-Type: application/json regardless of your Accept value.
Check your understanding
Answer these without looking back at the article.
- What happens if the client sends a request without a Content-Type header to a POST endpoint?
- How does the server set the Content-Type in the response?
- What is the difference between consumes and produces in @PostMapping and these headers?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.