Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Designing

API Mock

API Automated Testing

Sign up for free
Home / Tutorials / How to use the HTTP Accept Header?

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.

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:

  • The server does not have the content in the format that you requested
  • The server has the content in the format that you requested, but it is not available at the moment
  • The server has the content in the format that you requested, but it is not authorized to share it with you
  • The server has the content in the format that you requested, but it is too large or too complex to send back
  • The server does not understand or support the media-type that you requested

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:

  • 200 (OK): This means that the request was successful and the server sent back the exact type of content that you requested with the HTTP accept header. For example, if you send the request curl -H "Accept: text/html" https://example.com, and the server sends back an HTML document with the status code 200, this means that everything went well and you can use the HTML document as you wish.
  • 206 (Partial Content): This means that the request was successful and the server sent back a different type of content that you can also accept with the HTTP accept header. For example, if you send the request curl -H "Accept: text/html,application/json" https://example.com, and the server sends back JSON data with the status code 206, this means that the server could not provide HTML documents, but it could provide JSON data, which you can also accept. You can use the JSON data as you wish, but you should be aware that it is not the preferred type of content that you requested.
  • 406 (Not Acceptable): This means that the request failed and the server could not provide any type of content that you can accept with the HTTP accept header. For example, if you send the request curl -H "Accept: text/html" https://example.com, and the server sends back an error message with the status code 406, this means that the server does not have any HTML documents to send back, and it does not have any other type of content that you can accept. You cannot use the error message as you wish, but you should read it carefully and try to understand why the server could not send back the content that you requested. You may need to change your request or contact the server administrator for more information.
  • 415 (Unsupported Media Type): This means that the request failed and the server does not understand or support the media-type that you requested with the HTTP accept header. For example, if you send the request curl -H "Accept: application/x-custom" https://example.com, and the server sends back an error message with the status code 415, this means that the server does not know what application/x-custom is, and it cannot provide any content in that format. You cannot use the error message as you wish, but you should read it carefully and try to understand why the server does not support the media-type that you requested. You may need to change your request or contact the server administrator for more information.

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:

  • Click on the “New Request” button to create a new HTTP request.
Apidog
  • Select the HTTP method (GET, POST, PUT, etc.) from the dropdown menu and enter the URL of the API endpoint you want to test in the “URL” field then click on the “Headers” section to open it.
Apidog
  • You’ll typically see a list of headers with fields for “Name” and “Value”.
  • To add a new header, just click on the name and select the name and the content type you want to request as the value. For example, Accept: application/json means that you want to receive JSON data from the server.
  • Send your request to the server and you will see the response from the server in the “Response” section. You can inspect the status code, the response headers, and the response body.
  • To check if the server respects your HTTP accept header, look at the “Content-Type” header in the response. It should match one of the content types you requested in the request. For example, if you requested Accept: application/json, the response should have Content-Type: application/json.

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:

  • Use the quality value syntax to specify the preference order of the content types. For example, Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8 means that the client prefers HTML, then XHTML, then XML, and then any other type.
  • Use the wildcard character (*) to indicate that the client can accept any subtype or any type. For example, Accept: image/* means that the client can accept any image format, and Accept: */* means that the client can accept any content type.
  • Be specific about the content types that the client can handle, and avoid using Accept: */* unless necessary. This can help the server to send the most appropriate format for the client, and avoid sending unnecessary data that the client cannot process.
  • Test your HTTP accept header with different web servers and web services, and check the Content-Type response header to see if the server respects your preferences. You can use tools like curl or Apidog to send custom HTTP requests and inspect the response headers.

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

Join Apidog's Newsletter

Subscribe to stay updated and receive the latest viewpoints anytime.