UserPreferences

RestApiSupportSummary


This page summarizes how a REST-based API can be supported on popular web servers. Also a consideration is finding methods which will work for the common case where people have their sites hosted in a "virtual hosting" scenario where server reconfiguration is minimal or completely impossible.

  1. Apache
    1. CGI script with Extra Path Info
    2. mod_rewrite to a CGI script
    3. Configure Apache to Execute a Given Script
  2. Microsoft IIS
    1. Configuring IIS to allow PUT and DELETE
    2. Use ASP.NET to rewrite URLs
    3. Handling the Request from Script
      1. Handling PUT from ASP
      2. Handling PUT from ASP.NET

Apache

There are several methods of achieving this with Apache.

See also: [WWW]Dynamic Content with CGI (Apache Tutorial)

CGI script with Extra Path Info

This method can, it seems, be deployed in most situations where a user has access to install CGI scripts, and will likely work for non-CGI extensions such as PHP too, since they often "emulate" CGI. (Can someone verify this?)

No configuration is necessary; simply place a CGI script somewhere that it can be executed and use a URL such as this:

 http://www.mysite.com/interface.cgi/extra/path/info 

That will cause interface.cgi to be executed, with the request method and extra path information available in the environment variables REQUEST_METHOD and PATH_INFO respectively.

An entry's <link> (LinkElement, "One of possibly several persistent URI locations for the entry on the Web in the context and style of the publisher's website.") will most likely not be the same URI reference where the entry can be edited. The URI reference to where the entry can be edited will need to be provided in the AtomApi and likely should be in the <feed> (EchoExample).

See also: ContentNegotiation

mod_rewrite to a CGI script

If mod_rewrite is enabled on your server, you can configure it to remap all PUT and DELETE requests to a single script. This script can either use extra path info as above, or query string arguments.

Something similar to the following will achieve this:

RewriteCond %{REQUEST_METHOD} ^(PUT|DELETE)$ 
RewriteRule /(.*) /cgi-bin/putdeletehandler.cgi/$1

To do this from .htaccess when your site is not at the "root" of the URLspace, the RewriteBase directive can be used to tell mod_rewrite where your space begins.

This approach is simiar to the extra path info technique, but results in much cleaner apparent URLs.

(See also?)

Configure Apache to Execute a Given Script

This achieves the same as the mod_rewrite technique above but without requiring mod_rewrite to be installed. However, many server admins have the directive used here disabled in the server configuration.

Script PUT /cgi-bin/putdeletehandler.cgi 
Script DELETE /cgi-bin/putdeletehandler.cgi

Apache will only make use of these in preference to its internal mechanism for serving a request from the filesystem. If a CGI script or similar is encountered at the requested URL, it will take priority over the configured script regardless of request method.

See [WWW]the Script directive in the Apache manual for more.

Microsoft IIS

For IIS there are three things to be done. Firstly, your chosen scripting language must be configured to allow PUT and DELETE requests in the server configuration, and secondly the script must be allowed to execute for arbitrary URLs, since IIS does not seem to support the extra path info approach offered by Apache. Finally, the script must be able to access the request entity body to recieve the data.

Sadly, noone has yet found a way to handle this without access to reconfigure the server.

Configuring IIS to allow PUT and DELETE

This must be done regardless of which scripting language you use. Most will be configured by default to only allow GET and POST requests. This applies to both ASP and ASP.NET, which are the most commonly used under IIS.

You can reconfigure your chosen scripting system as follows:

(Adapted from RestAspExample)

Use ASP.NET to rewrite URLs

IIS can be configured to pass every request to ASP.NET and from there the ASP.NET application can either rewrite the URL or simply handle the request.

An example is given in a CodeProject article entitled [WWW]URL Rewriting with ASP.NET.

Handling the Request from Script

Handling PUT from ASP

It is possible to access the request entity body from ASP.

AsbjornUlsberg has prepared an example at RestAspExample.

Handling PUT from ASP.NET

AsbjornUlsberg has prepared an example at RestAspNetExample.


Original Author: MartinAtkins; please extend and correct :)

For reasoning on the use of PUT and DELETE, see CarrotVsOrange and CarrotVsOrangeDiscuss


CategoryApi, CategoryRest