web.d - make D functions accessible from the web easily

import arsd.web; class MySite : ApiProvider { string hello(string name) { return "Hello, " ~ name ~ "!"; } } mixin FancyMain!MySite;

See it live as a web page or as JSON.

web.d builds on top of cgi.d, dom.d and D's reflection capabilities (among other things) to automate the tedium involved in writing web sites and apis.

You start by implementing a class that inherits ApiProvider, then writing methods or child objects that are automatically exposed to the web, via URLs and web pages.

The return values of your methods are automatically formatted into a variety of formats for the user to consume. By automatically providing JSON and HTML, your functions can be made to make machine-consumable APIs as well as human usable web pages.

Hello, world explained

The hello world example (above) is a very short program, but has a lot of stuff going on under the hood.


By doing all this on each of your methods, adding new web functions is as simple as writing new D functions.

That's the main beauty of web.d: you're just writing functions, like natural D. You can often forget about the fact that you're writing web code at all!

mixin FancyMain!MySite

The FancyMain mixin (temporary name) is similar to [[GenericMain]] in cgi.d. Indeed, FancyMain simply provides a function that calls arsd.web.run(), then calls GenericMain itself.

The same rationale for GenericMain applies here (command line and web access without modification is one nice bonus), but I don't recommend it's use quite as strongly - if you have other needs, writing your own cgimain function and using GenericMain is still reasonably easy.

Anyway, you simply pass your subclass of ApiProvider to it, and it does the work of preparing reflection info and calling the function the user needs.

How the users can access your methods

High level libraries

Web.d provides automatically generated high level libraries to access it's functions from various languages.

Most the information that follows is implementation details so you can understand the pros and cons of the design as well as implement your own libraries, if desired.

URL structure

Basic named calls

Positional paramaters

Data types






Uploaded Files

Nested calls

One HTTP request can make several function calls on the server, providing a big speed boost. This requires no special code on your end.

Returned data

Envelope formats

See also: getGenericContainer

Data formats

See also: providing custom formats, customizing default formats.


See also: method attributes - pure

Built-in functions

XSRF protection

API Authentication

Accessing the CGI object

The Cgi object uses to communicate with the outside world is a member object of the base ApiProvider class. You can use it to do things like [[setting expiration dates]], [[working with http headers]], and more.

If web.d isn't right for a particular function, it tries to not get in the way of simply doing things the old fashioned way with cgi.d directly. Of course, you'll want to minimize this, to get the most of web.d, but it won't forbid it.

Reflection data

Automatic forms

Customizing the form

Returning HTML

Using the auto-generated Javascript library

If you're interested in more small Javascript, you may want to look at arsd.js, which provides some similar functions to dom.d as well as some simple solutions to common browser problems.

Graceful degradation