A Content-less PUT that returns 201 or 409
Thursday, 14th March, 2013
Thanks to people on the webmachine mailing-list, wmtrace, and the webmachine source (it really does implement the diagram!).
This webmachine resource accepts a PUT request to create a new resource. No content and no content headers are required in the request. A successful request returns 201, and the new resource is accessible at the same url as the PUT. If the resource already exists a 409 is returned.
-module(dragon_resource). -export([init/1, allowed_methods/2, content_types_accepted/2, accept_content/2, resource_exists/2 ]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, undefined}. allowed_methods(ReqData, Context) -> {['PUT'], ReqData, Context}. content_types_accepted(ReqData, Context) -> {[{"application/octet-stream", accept_content}], ReqData, Context}. accept_content(ReqData, Context) -> {false, ReqData, Context}. resource_exists(ReqData, Context) -> Name = wrq:path_info(name, ReqData), case dragon:create(Name) of ok -> NewReqData = wrq:set_resp_header("Location", wrq:path(ReqData), ReqData), {false, NewReqData, Context}; error -> {{halt, 409}, ReqData, Context} end.
The relevant dispatch.conf line is:
{["here", "be", "dragons", name], dragon_resource, []}.