Postman Tutorial Part 7 – Difference Between ‘Accept’ and ‘Content-Type’ HTTP Headers

“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:

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:

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



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

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.



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.

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.

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

3 thoughts on “Postman Tutorial Part 7 – Difference Between ‘Accept’ and ‘Content-Type’ HTTP Headers

  1. Hi,

    I set accept -> xml and content-type as json.
    Will there be any issue in this case? As i am expecting in xml and json both (accept value in xml format which specifies the format for response and content type which also specifies the format for request and response)

    Please help me to understand

  2. Thank you for sharing teh details.. and mentioning the interview question in the last.

    I selected the body type as JSON(application/json) and passed an XML request. I did not got any error and got the response from the server.

    I believe if you will get error or not depends on the server you are hitting too

Leave a Reply

Your email address will not be published. Required fields are marked *