Webmachine Post and Redirect
I've just started a new project using an Erlang backend that is built on the Basho Webmachine system.
I'm really loving the RESTful nature of Webmachine, but must admit it did take me a little while to figure out how to return a 403 redirect from a POST. I found the odd blog post (here and here) that sort of did what I wanted but not quite. Eventually, after a bit of debugging and much consulatation of The Diagram I came up with the following.
allow_missing_post(ReqData, State) ->
{true, ReqData, State}.
post_is_create(ReqData, State) ->
{true, ReqData, State}.
create_path(ReqData, State) ->
{"/my_redirect_location", ReqData, State}.
content_types_accepted(ReqData, State) ->
{[{"application/x-www-form-urlencoded", process_form}], ReqData, State}.
process_form(ReqData, State) ->
%% do stuff
{true, wrq:do_redirect(true, ReqData), State};
So I think you need to have the allow_missing_post and post_is_create functions return true and the create_path function return the path you want to redirect to.
Then you need to have the content type mapped to a handler function which calls the request modification function "do_redirect".
Oh, also I'm probably reinventing the wheel (and probably badly) but I wrote my a couple of functions to turn a POSTed form into a proplist for processing in the handler.
parse_form_body(FormBody) ->
Tokens = binary:split(FormBody, <<"&">>, [global]),
lists:foldl(fun(X, Acc) -> add_to_proplist(X, Acc) end, [], Tokens).
add_to_proplist(Item, Items) ->
[Key, Value] = binary:split(Item, <<"=">>, [global]),
[{Key, Value}|Items].