HTTP

Header

Accept Header

Learn how the Accept header tells servers which content types (JSON, HTML, XML) your client can handle. Master content negotiation and quality values.

4 min read beginner Try in Playground

TL;DR: Tells servers what content types your client can handle (JSON, HTML, XML, etc.). Use for content negotiation so servers return the format you prefer.

What is Accept?

The Accept header tells the server what content types (media types) your client can understand and process. It’s like telling a restaurant “I can eat vegetarian or seafood dishes” so they know what to serve you.

Servers use this information to choose the best format for their response through a process called content negotiation.

How Accept Works

Client specifies preferred formats:

GET /posts/42 HTTP/1.1
Host: api.example.com
Accept: application/json, application/xml;q=0.8, text/plain;q=0.5
```text

**Server responds with the best match:**

```http
HTTP/1.1 200 OK
Content-Type: application/json

{"id": 42, "title": "My Post"}

Syntax and Quality Values

Basic Syntax

Accept: type/subtype
Accept: type/subtype, type/subtype
Accept: type/*
Accept: */*
```text

### Quality Values (q-values)

Quality values indicate preference from 0.0 (not acceptable) to 1.0 (most preferred):

```http
Accept: application/json;q=1.0, application/xml;q=0.8, text/plain;q=0.5

This means:

  • JSON is most preferred (q=1.0, default)
  • XML is acceptable (q=0.8)
  • Plain text is least preferred (q=0.5)

Common Examples

Web Browsers

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
```text

Browsers prefer HTML, then XHTML, then XML, then WebP images, then anything else.

### API Clients

```http
Accept: application/json

Most APIs prefer JSON for structured data.

Mobile Apps

Accept: application/json, image/jpeg, image/png
```text

Mobile apps often need both data and images.

### RSS Readers

```http
Accept: application/rss+xml, application/atom+xml, application/xml;q=0.8

Feed readers prefer RSS or Atom formats.

Content Type Categories

Application Types

Accept: application/json          # JSON data
Accept: application/xml           # XML data
Accept: application/pdf           # PDF documents
Accept: application/zip           # Compressed files
Accept: application/octet-stream  # Binary data
```http

### Text Types

```http
Accept: text/html        # HTML pages
Accept: text/plain       # Plain text
Accept: text/css         # Stylesheets
Accept: text/javascript  # JavaScript code
Accept: text/csv         # CSV data

Image Types

Accept: image/jpeg       # JPEG images
Accept: image/png        # PNG images
Accept: image/gif        # GIF images
Accept: image/webp       # WebP images
Accept: image/svg+xml    # SVG graphics
```http

### Video/Audio Types

```http
Accept: video/mp4        # MP4 videos
Accept: audio/mpeg       # MP3 audio
Accept: audio/wav        # WAV audio

Wildcards

Accept Any Subtype

Accept: image/*
```text

Accepts any image format (JPEG, PNG, GIF, etc.)

### Accept Anything

```http
Accept: */*

Accepts any content type (fallback option)

Mixed Wildcards

Accept: application/json, image/*, text/plain
```text

Prefers JSON, accepts any image, or plain text as fallback.

## Real-World Scenarios

### API Version Negotiation

```http
Accept: application/vnd.api+json;version=2, application/json;q=0.8

Request API v2 format, fall back to standard JSON.

Language-Specific Content

Accept: application/json, application/vnd.company.resource+json
```text

Prefer custom company format, fall back to standard JSON.

### Multi-Format Support

```http
Accept: application/json;q=1.0, application/xml;q=0.8, text/csv;q=0.6, */*;q=0.1

Comprehensive preference list with fallbacks.

Server Response Strategies

Exact Match

# Client requests
Accept: application/json

# Server responds
Content-Type: application/json
```text

### Best Available Match

```http
# Client requests
Accept: application/json, application/xml;q=0.8

# Server only has XML
Content-Type: application/xml

No Acceptable Format

# Client requests
Accept: application/pdf

# Server only has JSON
HTTP/1.1 406 Not Acceptable
Content-Type: application/json

{
  "error": "Requested format not available",
  "available": ["application/json", "application/xml"]
}
```text

## Best Practices

### For Clients

**1. Be specific about what you can handle**

```http
# ❌ Too vague
Accept: */*

# ✅ Specific preferences
Accept: application/json, application/xml;q=0.8

2. Include fallback options

Accept: application/json, text/plain;q=0.5, */*;q=0.1
```text

**3. Use quality values for preferences**

```http
Accept: application/json;q=1.0, application/xml;q=0.8, text/csv;q=0.6

For Servers

1. Respect client preferences

app.get('/data', (req, res) => {
  const acceptHeader = req.headers.accept

  if (acceptHeader.includes('application/json')) {
    res.json(data)
  } else if (acceptHeader.includes('application/xml')) {
    res.type('xml').send(toXML(data))
  } else if (acceptHeader.includes('text/csv')) {
    res.type('csv').send(toCSV(data))
  } else {
    res.status(406).json({ error: 'Format not supported' })
  }
})
```text

**2. Provide helpful 406 responses**

```javascript
res.status(406).json({
  error: 'Not Acceptable',
  message: 'Requested format not available',
  available: ['application/json', 'application/xml', 'text/csv']
})

Common Patterns

Progressive Enhancement

Accept: application/vnd.api+json, application/json;q=0.9, */*;q=0.1
```text

Try custom format first, fall back to standard JSON, then anything.

### Format Negotiation

```http
Accept: application/json, application/xml;q=0.8, text/html;q=0.6

API that can also serve web pages.

Strict Requirements

Accept: application/json
```text

Only accept JSON, no fallbacks.

## Browser Defaults

Most browsers send complex Accept headers:

```http
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

This tells servers the browser prefers HTML but can handle many formats.

Testing Accept Headers

Using curl

# Request JSON
curl -H "Accept: application/json" https://api.example.com/posts

# Request XML with fallback
curl -H "Accept: application/xml, application/json;q=0.8" https://api.example.com/posts

# Request specific version
curl -H "Accept: application/vnd.api+json;version=2" https://api.example.com/posts
```text

### Using JavaScript

```javascript
fetch('/api/posts', {
  headers: {
    Accept: 'application/json, application/xml;q=0.8'
  }
})

Frequently Asked Questions

What is the Accept header?

Accept tells the server which content types the client can handle. The server uses this for content negotiation to choose the best response format.

How do I request JSON from an API?

Send Accept: application/json. The server should return JSON if it supports it. Some APIs require this header; others default to JSON.

What does the quality value (q) mean?

Quality values indicate preference from 0 to 1. Accept: text/html, application/json;q=0.9 prefers HTML but accepts JSON. Higher q means higher preference.

What happens if the server cannot match Accept?

The server may return 406 Not Acceptable, or ignore Accept and return its default format. Behavior varies by server implementation.

Keep Learning