Created
          August 24, 2012 10:30 
        
      - 
      
 - 
        
Save w495/3448917 to your computer and use it in GitHub Desktop.  
    handler jsonapi test
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | -module(handler_jsonapi_test). | |
| -behaviour(cowboy_http_handler). | |
| -export([init/3, handle/2, terminate/2]). | |
| -define(AUTH_COOKIE_NAME, <<"auth_mceed0btegt1xa">>). | |
| init(_, Req, _Opts) -> | |
| {ok, Req, undefined}. | |
| handle(Req, State) -> | |
| io:format("handle ~p ~n", [?LINE]), | |
| {Code, Repdata, Reqres} = case cowboy_http_req:method(Req) of | |
| {'POST', Req1} -> | |
| handle_post(Req1, State); | |
| {_, Req1} -> | |
| {405, <<"{\"error\": \"method_not_allowed\"}">>, Req1} | |
| end, | |
| io:format("handle ~p ~n", [?LINE]), | |
| Reply = cowboy_http_req:reply(Code, | |
| [{<<"Content-Type">>, <<"application/json;charset=UTF-8">>}], | |
| Repdata, | |
| Reqres | |
| ), | |
| io:format("handle ~p ~n", [?LINE]), | |
| {ok, Reply, State}. | |
| handle_post(Req, State)-> | |
| io:format("handle_post ~p ~n", [?LINE]), | |
| X = cowboy_http_req:body_qs(Req), | |
| io:format("~n cowboy_http_req:body_qs(Req) = ~p~n~n", [X]), | |
| io:format("handle_post ~p ~n", [?LINE]), | |
| {Pbody, Req1} = X, | |
| io:format("handle_post ~p ~n", [?LINE]), | |
| handle_post_body(Req1, State, Pbody). | |
| handle_post_body(Req, State, Pbody)-> | |
| io:format("handle_post_body ~p ~n", [?LINE]), | |
| case proplists:get_value(<<"data">>, Pbody) of | |
| undefined -> | |
| {510, <<"{\"error\": \"no_data\"}">>, Req}; | |
| Data -> | |
| handle_data(Req, State, Data) | |
| end. | |
| handle_data(Req, State, Data)-> | |
| io:format("handle_data ~p ~n", [?LINE]), | |
| {_aobj, Req1} = auth(Req), | |
| {200, Data, Req1}. | |
| terminate(_Req, _State) -> | |
| ok. | |
| auth(Req)-> | |
| auth_cookie(Req). | |
| auth_cookie(Req)-> | |
| cowboy_http_req:cookie(?AUTH_COOKIE_NAME, Req). | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
curl http://127.0.0.1:8000/jsonapi/test -d "data=%7B%22name%22%3A+%22signin%22%2C+%22conf%22%3A%7B%22position%22%3A1%2C+%22comment%22%3A%22\u10d0\u30a4\u10ea\u10ef\u10d8\u0399\u3042\u03c8\u10dc\u3078\u03c5\u10e1\u10d0\u10dc\u10db\u03a4\u10da\u03c6\u30df\u03c1\u039a\u30df\u03a9\u03b8\u039e\u10dc\u3051\u3093\u0395\u10d0\u30e0\u30a4\u10d8\u10dc\u30bf\u306b\u10e0\u10d0\u03c1\u10d8\u3064\u03a8\u10d4\u03b9\u30b7\u10f0\u039c\u306e\u306d\u03c5\u10d0%22%7D%7D"