Content negotiation in Web API(accept and content-type attribute of header)

Content negotiation is a way to serve client-specific representations of the same resource.
A client might send this HTTP request:

GET http://localhost.:21069/api/products/1 HTTP/1.1
Host: localhost.:21069
<strong>Accept</strong>: application/json, text/javascript, */*; q=0.01

In response, the server might send:

HTTP/1.1 200 OK
<strong>Content-Type:</strong> application/json; charset=utf-8
Content-Length: 57
Connection: Close

The Accept header is used by HTTP clients to tell the server what content types they’ll accept. The server will then send back a response, which will include a Content-Type header telling the client what the content type of the returned content actually is.

However, as you may have noticed, HTTP requests can also contain Content-Type headers. Why? Well, think about POST or PUT requests. With those request types, the client is actually sending a bunch of data to the server as part of the request, and the Content-Type header tells the server what the data actually is (and thus determines how the server will parse it).

In particular, for a typical POST request resulting from an HTML form submission, the Content-Type of the request will normally be either application/x-www-form-urlencoded or multipart/form-data.

Share the joy
  •  
  •  
  •  

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.