Which header field indicates the media type of response that the REST client expects?

“Accept” and “Content-Type” are HTTP Headers which control input and output formats. “Accept” is a request header and “Content-Type” is both request and response header. Let’s see the difference between these headers:

  • Accept:
  • Content-Type:
  • Author: Amod Mahajan

Accept:

Let’s understand it using a real time example. We must have submitted final year projects in our college and there were a defined format to submit it. For example say format was PDF or printed and binded book. A defined Format makes it easier to understand. The same case is with HTTP methods. When a client sends a request to a server, server returns response and this response may be in any format. So client may mention desired response format using “Accept” request header. If no Accept header is present in the request, the server sends default or pre-configured representation type. So mentioning “Accept” header is optional in request.

As per RFC-7231, The “Accept” header field can be used by user agents to specify response media types that are acceptable. Accept header fields can be used to indicate that the request is specifically limited to a small set of desired types. Some values of Accept headers are application/json , application/pdf, image/gif etc.

Let’s see Accept header in action:

  1. When we don’t mention “Accept” header:
Which header field indicates the media type of response that the REST client expects?

2. When we mention “Accept” header as “application/xml”:

Which header field indicates the media type of response that the REST client expects?

3. When we mention “Accept” header with format which requested API doesn’t support:

Which header field indicates the media type of response that the REST client expects?

Content-Type:

We need to send request body to server to get a response. Request body can be JSON , xml , file etc. So using Content-Type header in request headers, we mention in which format we are sending data to server. Some values of Content-Type headers are application/json , application/pdf etc.

In below image, we can see that I have set “Content-Type” as “application/json” and in Body I am passing json data.

Which header field indicates the media type of response that the REST client expects?
Which header field indicates the media type of response that the REST client expects?

If there is mismatch in mentioned Content-Type and actual type of Request body, it may throw an error depends on behavior of requested API.

Which header field indicates the media type of response that the REST client expects?
Which header field indicates the media type of response that the REST client expects?

Note: Content-Type is not only request header. Server also sends this header as response header with the value as asked format or default format.

Which header field indicates the media type of response that the REST client expects?

Note- If we mention “Content-Type” in a GET request, we will not get any error as GET method does not have any body so it is meaningless. It was asked in interview so mentioned here.

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyApiTesting

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

What is Content

The Content-Type field in the HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API.

Which HTTP header specifies the media type of the response?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.

What is a response header field?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.

Which HTTP request header should a client use to advertise the media type S it is able to understand?

The Accept request HTTP header indicates which content types, expressed as MIME types, the client is able to understand.