Skip to content

Instantly share code, notes, and snippets.

@greyson
Created May 8, 2012 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyson/2631569 to your computer and use it in GitHub Desktop.
Save greyson/2631569 to your computer and use it in GitHub Desktop.
-module( bad_redirect_on_create ).
-export([ init/1,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
create_path/2,
post_is_create/2,
from_json/2,
to_json/2 ]).
-include_lib( "webmachine/include/webmachine.hrl" ).
init([]) -> {ok, undefined}.
allowed_methods( R, S ) ->
{['GET', 'HEAD', 'POST'], R, S }.
content_types_accepted( ReqData, State ) ->
{[ {"application/json", from_json} ], ReqData, State}.
content_types_provided( ReqData, State ) ->
{[ {"application/json", to_json} ], ReqData, State}.
post_is_create( R, S ) ->
{true, R, S}.
create_path( R, _S ) ->
NewId = <<"TestDevice">>,
{binary_to_list( NewId ), R, NewId }.
from_json( ReqData, NewId) ->
ReturnIo = mochijson2:encode( {struct, [{id, NewId}]} ),
ReturnJson = iolist_to_binary( ReturnIo ),
R2 = wrq:append_to_resp_body( ReturnJson, ReqData ),
{true, R2, NewId}.
to_json( ReqData, UserJson ) ->
ReturnIo = mochijson2:encode( {struct, []} ),
ReturnJson = iolist_to_binary( ReturnIo ),
{ReturnJson, ReqData, UserJson}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment