Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does the Accept header differ from Content-Type?

Quick answer

Accept tells a server which response media types the client can handle or prefers. Content-Type identifies the media type of a message body: in a request, it describes the data being sent; in a response, it describes the data returned. For example, a client can send JSON and ask for JSON back using both headers. They answer different questions, so one does not replace the other. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I keep mixing these up. What does each header mean in plain language?

Geez

Think of two separate questions. Accept says, “What response formats can I handle or prefer?” Content-Type says, “What format is the content in this message?” In a request, Content-Type describes the body you are sending; in a response, it describes the body returned. Accept expresses a response-format preference, not a label for the request body. [S1] [S2]

So if you send a JSON request and want a JSON response, both headers may appear, but they describe different things.

Follow-up question

Can you show both headers together in one request and explain what I should look for in the reply?

Geez

Here is a simplified example. Assume the endpoint accepts the JSON body shown and can return JSON; those are example conditions, not guarantees about any particular service.

POST /items HTTP/1.1
Accept: application/json
Content-Type: application/json

{"name":"notebook"}

The request’s Content-Type labels the JSON body being sent. Accept expresses that the client can handle, and is asking for, a JSON response. If the server returns a response body, check that response’s Content-Type to see how the returned content is identified. The server selects a response format from the formats proposed through Accept and identifies its chosen format with Content-Type. [S1] [S2]

This example doesn’t establish that a real endpoint supports JSON, that it will return JSON, or what it will do if it cannot meet that preference. Check that service’s documentation and the actual response.

Follow-up question

What is the quickest way to diagnose a mix-up when a request or response seems to have the wrong format?

Geez

Check the direction and the body separately:

  1. Sending a body? Read the request’s Content-Type. Ask whether it describes the body actually sent.
  2. Requesting a response format? Read Accept. Ask whether it lists a format the client can handle or prefers.
  3. Received a body? Read the response’s Content-Type. That describes the returned content—not the client’s earlier preference. [S1] [S2]

For example, if the request body is JSON but its Content-Type says text/plain, investigate the request’s body label. If Accept says application/json but the response is identified as another type, compare the response with the preference you sent. Accept is negotiation input, not a guarantee that the server will return the preferred type. [S1]

Follow-up question

Could you give me a reusable mini-checklist, including what to inspect if there is no request body?

Geez

Use this before changing a header:

  • Locate the message you’re checking: request or response.
  • If it has a body, compare that body with its Content-Type.
  • For a request, read Accept as the response format or formats the client can handle or prefers.
  • If there is a response body, inspect the response’s Content-Type to identify its media type.
  • If there is no request body, don’t treat Accept as a substitute body label; check the service’s instructions for which headers it expects.

Quick rule: Content-Type describes content in this message; Accept expresses a response-format preference. Accept values can include specific media types, wildcards, and quality weights. Which formats a service supports, and how it handles every negotiation case, depends on that service. [S1] [S2]

A practical checklist

  • Identify whether you are inspecting a request or a response.

  • If a message has a body, compare it with that message’s Content-Type.

  • Read a request’s Accept as a response-format preference, not a request-body label.

  • Check a response’s Content-Type to identify the format of its returned body.

  • Consult the specific service’s documentation for supported formats and expected headers.

A few more useful answers

Does Accept guarantee the server will return the format I request?

No. Accept expresses formats the client can handle or prefers; it is not a guarantee that the server will return one of them. Check the actual response and the service’s documentation. [S1]

Which header describes JSON I send in a request body?

Content-Type describes the request body’s media type, so for a JSON body the example value is application/json. Whether a particular endpoint accepts it depends on that service. [S2]

Where do I check the format of the data returned?

Inspect Content-Type in the response; it identifies the media type of the returned content. Accept in the request expresses the client’s response-format preference. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Accept header — HTTP | MDN
    developer.mozilla.org · Checked
  2. [S2] Content-Type header — HTTP | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work