Creating then accessing Mochiweb Cookies

168 views Asked by At

Hey I'm trying to have the website I'm making send a cookie to the user then display a webpage.

So I've found http://alexmarandon.com/articles/mochiweb_tutorial/ as the only real tutorial about how make a cookie but it seems to error out for me.

My loop looks something like this (my make_cookie, get_cookie_value, render_ok and get_username are the same as his except I use 'mename' as the key instead of 'username') :

loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
try
    case dispatch(Req, valid_urls:urls()) of
        none -> 
            case filelib:is_file(filename:join([DocRoot, Path])) of
                true -> 
        %% If there's a static file, serve it
                    Req:serve_file(Path, DocRoot);
                false ->
        %% Otherwise the page is not found
                    case Req:get(method) of
                        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
                            case Path of
                                "response" ->
                                    QueryStringData = Req:parse_qs(),

                                    Username = get_username(Req, QueryStringData),
                                    Cookie = make_cookie(Username),

                                    FindCookie = get_cookie_value(Req,"mename","Not Found."),
                                    % render_ok(Req, [Cookie], greeting_dtl, [{username, Username}]),

                                    Req:respond({200, [{"Content-Type", "text/html"}],
                                                "<html><p>Webpage</p></hmtl>"});
                                _ ->
                                    Req:not_found()
                            end
                    end
            end;
        Response -> 
            Response
    end
catch
Type:What ->
        Report = ["web request failed",
                  {path, Path},
                  {type, Type}, {what, What},
                  {trace, erlang:get_stacktrace()}],
        error_logger:error_report(Report),
        %% NOTE: mustache templates need \ because they are not awesome.
        Req:respond({500, [{"Content-Type", "text/plain"}],
                     "request failed, sorry\n"})
end.

The error I get is:

[error] "web request failed", path: "response", type: error, what: undef, trace: [{greeting_dtl,render,[[{username,"GET"}]],[]}

The error seems to be coming from render_ok, but being new to Erlang-mochiweb I'm not sure on how to fix this.

1

There are 1 answers

0
tkowal On BEST ANSWER

You have render_ok commented out, but the file was not compiled after that. The error message suggests, that it still calls greeting_dtl.

I assume you are using erlydtl, because the error is in greeting_dtl. Errors in render function usually mean, that you misspelled some property name. Check, where your template is and if it has something like {{ something.username }}. Comment it out or change to mename (whatever makes sense).