Server implementation

Content negotiation implementations

Apache server

The Apache Hypertext Transfer Protocol Daemon (HTTPD) can select the best representation of a resource using content negotiation as defined in HTTP/1.1.This is done using client preferences for media type, language, character set and finally encoding. The mod_negotiation module is the one that provides content negotiation, and it is compiled by default. HTTPD supports server-based content negotiation, Accept, Accept-Language, Accept-Charset and Accept-Encoding headers [47]. It also supports transparent content negotiation, but not feature negotiation [6].There are two ways to provide the server with the required information on each of the variants [1]:
  • With a type-map file First, a file suffix must be defined as type-map using a handler defined in the server configuration file, usually expressed as follows:
AddHandler type-map .var
Each type-map file describing a resource must have the same name, plus the .var extension. The description contains the media type, language, en-coding, character set. Also the source qualities which indicate the relative quality of the described variant compared to the others available.
  • Multiviews Using the per-directory option MultiViews with an Options directive, allows the server in the case of the absence of the /some/dir/foo resource to search for an existing representation named foo.*. The MultiViews option must be set explicitly even if the Options All directive is present.

Spring framework

Spring also provides an implementation of content negotiation in a Spring MVC project, primarily to determine the media type of a request. This is achieved by one of these techniques [6]:
  • The URI suffixes (extensions) In the requested URI, the preferred media type can be passed using an extension (e.g. .xml/.json). The framework can check for the existence of a path extension, and for a service providing employee information, the response to the following request:
curl http://some/uri/employee/10.json
is the json representation of this employee. And if xml is the desired representation, the request should end with the xml extension:
curl http://some/uri/employee/10.xml
The favorPathExtension value must be set to true to enable this approach.
  • The URI parameter Similar to the previous technique, this time instead of suffixing the URI with an extension, a defined favorite parameter is used,e.g. ”format”, and the previous requests would look like this:
curl http://some/uri/employee/10?mediaType=json
and
curl http://some/uri/employee/10?mediaType=xml
The favorParameter value must be set to true and set a preferred parameterName to enable this approach.
  • The Accept header If the Accept header is enabled, Spring MVC will look up its value in the incoming request to determine the representation type. The ignoreAcceptHeader value must be set to false to enable this approach.
When developing a REST service, the @RequestMapping annotation provides the consume and produce parameters to specify the type of media it accepts as input or provides as output.
References
[{{ reference.id }}] : {{ reference.title }}
Footnotes
[1] : Apache HTTP Server: Content Negotiation.