How to use the HTTP Accept Header?

The HTTP Accept header is a request header that tells the server what content types the client can understand. Learn how to use it for content negotiation and see some examples.

Ashley Innocent

Ashley Innocent

9 May 2025

How to use the HTTP Accept Header?

If you are a web developer, you probably know how important it is to communicate effectively with the web servers that host your applications. You need to send the right requests and receive the right responses, so that your applications can function properly and provide a great user experience. But do you know how to use the HTTP accept header to optimize this communication and make your applications more flexible and efficient? If not, don’t worry.

In this blog post, I will teach you everything you need to know about the HTTP accept header and how to use tools like Apidog to test and debug your HTTP requests and responses.

button

What is the HTTP Accept Header and How Does It Work?

The HTTP accept header is a part of the request message. It is a way for the client to tell the server what kind of content it can accept and process. The content can be anything that the server can provide, such as HTML, XML, JSON, images, videos, audio, etc. The HTTP accept header has the following format:

Accept: media-type, media-type, ...

The media-type is a string that specifies the type and subtype of the content, such as text/html, application/json, image/jpeg, etc. You can also use wildcards to indicate any type or subtype, such as */*, text/*, image/*, etc. You can also use parameters to provide more details about the content, such as the quality, the language, the encoding, etc. For example, text/html;q=0.8,en-US means that the client can accept HTML documents with a quality of 0.8 (out of 1) and in US English.

You can specify multiple media-types in the HTTP accept header, separated by commas. The order of the media-types indicates the preference of the client. The first media-type is the most preferred, the second is the second most preferred, and so on. For example, Accept: text/html,application/json,image/png means that the client prefers HTML documents, then JSON data, then PNG images.

What Are HTTP Headers? (A Comprehensive Guide)
HTTP headers are the unsung heroes of the World Wide Web. They are essential components of the HTTP protocol. In simple terms, HTTP headers are metadata that accompany every HTTP request and response, providing crucial information about the data being sent or received.

Why HTTP Accept Header is important?

The HTTP accept header is a powerful way of negotiating the content between the client and the server. It allows the client to request different types of content based on its capabilities and preferences, and it allows the server to deliver the best possible content for the client, based on its availability and compatibility. This can improve the performance, efficiency, and user experience of both the client and the server.

HTTP accept header is important because it helps the server deliver the best possible content for the client, based on its capabilities and preferences. It also helps the server avoid sending unnecessary or incompatible data that the client cannot use or display. This can improve the performance, efficiency, and user experience of both the client and the server.

How to Use the HTTP Accept Header to Request Different Types of Content from the Server

To write the HTTP accept header in your request message, you need to use a tool or a library that allows you to send HTTP requests. There are many tools and libraries available for different languages and platforms, such as curl, Apidog, Axios, Fetch, etc. For this example, I will use curl, which is a command-line tool that you can use to send HTTP requests and receive HTTP responses.

To use curl, you need to type the following command in your terminal:

curl -H "Accept: media-type, media-type, ..." URL

The -H option allows you to add a header to your request. The Accept: media-type, media-type, ... is the HTTP accept header that you want to send. The URL is the address of the server that you want to request data from. For example, if you want to request HTML documents from https://example.com, you can type:

curl -H "Accept: text/html" https://example.com

This will send a request to https://example.com with the header Accept: text/html, meaning that you can only accept HTML documents as a response.

To read the response message from the server, you need to look at the status code, the content-type header, and the body of the message.

For example, if you send the request curl -H "Accept: text/html" https://example.com, you might receive the following response:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8

<html>
<head>
<title>Example Domain</title>
</head>
<body>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</body>
</html>

The status code is 200, meaning that the request was successful. The content-type header is text/html; charset=UTF-8, meaning that the server sent back an HTML document with UTF-8 encoding.

You can use the HTTP accept header to request different types of content from the server by changing the media-type in your request. For example, if you want to request JSON data from https://example.com, you can type:

curl -H "Accept: application/json" https://example.com

This will send a request to https://example.com with the header Accept: application/json, meaning that you can only accept JSON data as a response. You might receive the following response:

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

{
  "domain": "example.com",
  "purpose": "illustrative examples in documents",
  "link": "https://www.iana.org/domains/example"
}

The status code is 200, meaning that the request was successful. The content-type header is application/json, meaning that the server sent back JSON data. The body of the message is the JSON data itself, which you can see in your browser or in the Apidog website.

You can also request multiple types of content from the server by specifying more than one media-type in your request. For example, if you want to request either HTML documents or JSON data from https://example.com, you can type:

curl -H "Accept: text/html,application/json" https://example.com

How to Use HTTP Authorization Header ?
Learn how to use HTTP authorization header to access APIs securely and efficiently, and how to handle common errors and challenges with it.

How to Handle Different Types of Responses from the Server Based on the HTTP Accept Header

Sometimes, the server may not be able to send back the exact type of content that you requested with the HTTP accept header. This can happen for various reasons, such as:

There are many possible status codes that the server can send back, but here are some of the most common ones that you may encounter when using the HTTP accept header:

The content-type header and the body of the response message may also vary depending on the status code and the media-type that the server sent back. You can use tools like Apidog to view the content-type header and the body of the message in a user-friendly way.

How to use Apidog to test and debug my HTTP accept header?

Apidog is a web-based tool that helps you test, debug, and document your APIs for free.

button

To use Apidog to test and debug your HTTP accept header, you need to follow these steps:

Apidog
Apidog

Best practices and tips for using the HTTP accept header.

The HTTP accept header is a request header that indicates which content types, expressed as MIME types, the client is able to understand. The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Type response header. Here are some best practices and tips for using the HTTP accept header in your web development projects:

Conclusion

In this post, I have explained the importance of the HTTP Accept header and how it can be used to negotiate the content type between the client and the server. I have also shown how to set the Accept header in different scenarios.

By using the Accept header, we can ensure that the server responds with the most appropriate format for our needs, and avoid unnecessary conversions or errors. The Accept header is one of the many HTTP headers that can help us build more robust and efficient web applications.

button

Explore more

Apidog SEO Settings Explained: Maximize Your API Docs Visibility

Apidog SEO Settings Explained: Maximize Your API Docs Visibility

Discover how to supercharge your API documentation's visibility with Apidog's powerful SEO features. This comprehensive guide covers everything from page-level optimizations like custom URLs and meta tags to site-wide settings such as sitemaps and robots.txt.

18 June 2025

How to Protect API Specification from Unauthorized Users with Apidog

How to Protect API Specification from Unauthorized Users with Apidog

Learn how Apidog empowers you to protect API specification from unauthorized users. Explore advanced API documentation security, access controls, and sharing options for secure API development.

17 June 2025

How to Use the PostHog MCP Server?

How to Use the PostHog MCP Server?

Discover how to use the PostHog MCP server with this in-depth technical guide. Learn to install, configure, and optimize the server for seamless PostHog analytics integration using natural language. Includes practical use cases and troubleshooting.

16 June 2025

Practice API Design-first in Apidog

Discover an easier way to build and use APIs