title: case_clause error after successful response [cowboy rest]
body: <p>my erlang server generates a case_clause error after every request for some reason. Here is my code:<br>
  <strong>keychain_no_auth_handler.erl</strong></p>
  
  <pre><code>-module(keychain_no_auth_handler).
  -export(
    [ init/3, 
      content_types_accepted/2,
      terminate/3,
      allowed_methods/2,
      handle_request/2
    ]).
  
  -export([is_registered/1]).
  
  init(_Transport, _Req, []) -&gt; {upgrade, protocol, cowboy_rest}.
  
  terminate(_Reason, _Req, _State) -&gt; ok.
  
  allowed_methods(Req, State) -&gt; {[&lt;&lt;"POST"&gt;&gt;], Req, State}.
  
  content_types_accepted(Req, State) -&gt; {[{&lt;&lt;"application/json"&gt;&gt;, handle_request}], Req, State}.
  
  handle_request(Req, State) -&gt;
      {Action, Req2} = cowboy_req:binding(action, Req),
  
      {Reply, Req3} = case Action of
          &lt;&lt;"registered"&gt;&gt; -&gt; is_registered(Req2)
      end,
  
      {ok, Req4} = cowboy_req:reply(200, [{&lt;&lt;"content-type"&gt;&gt;, &lt;&lt;"application/json"&gt;&gt;}], Reply, Req3),
     {ok, Req4, State}
  .
  
  is_registered(Req) -&gt;
      {Number, Req2} = cowboy_req:qs_val(&lt;&lt;"number"&gt;&gt;, Req),
      Reply = backend:is_registered(Number),
      {Reply, Req2}
  .
  </code></pre>
  
  <p><strong>is_registered [function]</strong></p>
  
  <pre><code>is_registered(Number) -&gt;
      emysql:prepare(is_registered, &lt;&lt;"SELECT `device`.`id` FROM `device` WHERE `device`.`number`= ?"&gt;&gt;),
      ResultPacket = emysql:execute(auth_pool, is_registered, [Number]),
      ResultType = is_record(ResultPacket, result_packet),
  
      case ResultType of
          true -&gt; 
              Field = emysql_util:as_proplist(ResultPacket),
              Id = proplists:get_value(&lt;&lt;"device.id"&gt;&gt;, Field),
              Registered = Id &gt;= 0,
  
              case Registered of
                  true -&gt; 
                      Reply = {[{response, registered}]},
                      jiffy:encode(Reply)
                  ;
                  false -&gt; 
                      Reply = {[{response, available}]},
                      jiffy:encode(Reply)
              end;
          false -&gt; 
              Reply = {[{response, error}]},
              jiffy:encode(Reply)
      end
  .
  </code></pre>
  
  <p>The server replies with the appropriate response and then generates a case_clause error after every request:</p>
  
  <blockquote>
    <p>=ERROR REPORT==== 5-Feb-2014::10:32:39 === Error in process &lt;0.182.0> on node 'keychain@dnldd' with exit value:
    {{case_clause,{ok,{http_req,#Port&lt;0.2286>,ranch_tcp,keepalive,&lt;0.182.0>,&lt;&lt;4
    bytes>>,'HTTP/1.1',{{127,0,0,1},49720},&lt;&lt;9 bytes>>,undefined,4004,&lt;&lt;11
    bytes>>,undefined,&lt;&lt;0 bytes>>,[],[{action,&lt;&lt;10 bytes>>}],[{&lt;&lt;4
    bytes>>,&lt;&lt;14 bytes>>},{&lt;&lt;10 bytes>>,&lt;&lt;10 bytes>>},{&lt;&lt;14 bytes>>,&lt;&lt;1
    byte>>},{&lt;&lt;13 bytes>>,&lt;&lt;8 bytes>>},{&lt;&lt;6 bytes>>,&lt;&lt;51 bytes>>},{&lt;&lt;6
    bytes>>,&lt;&lt;12 bytes>>},{&lt;&lt;12 bytes>>,&lt;&lt;16 bytes>>},{&lt;&lt;10 bytes>>,&lt;&lt;120
    bytes>>},{&lt;&lt;13 bytes>>,&lt;&lt;36 bytes>>},{&lt;&lt;6 bytes>>,&lt;&lt;3 bytes>>},{&lt;&lt;3
    bytes>>,&lt;&lt;1 byte>>},{&lt;&lt;15 bytes>>,&lt;&lt;17 bytes>>},{&lt;&lt;15 bytes>>,&lt;&lt;14
    bytes>>}],[{&lt;&lt;12 bytes>>,{&lt;&lt;11 bytes>>,&lt;&lt;4 bytes>>,[]}},{&lt;&lt;17
    bytes>>,undefined},{&lt;&lt;13 bytes>>,undefined},{&lt;&lt;19
    bytes>>,undefined},{&lt;&lt;8 bytes>>,undefined},{&lt;&lt;6 bytes>>,[... </p>
    
    <p>=ERROR REPORT==== 5-Feb-2014::10:32:39 === Ranch listener keychain_http had connection process started with
    cowboy_protocol:start_link/4 at &lt;0.182.0> exit with reason:
    {{case_clause,{ok,{http_req,#Port&lt;0.2286>,ranch_tcp,keepalive,&lt;0.182.0>,&lt;&lt;"POST">>,'HTTP/1.1',{{127,0,0,1},49720},&lt;&lt;"127.0.0.1">>,undefined,4004,&lt;&lt;"/registered">>,undefined,&lt;&lt;>>,[],[{action,&lt;&lt;"registered">>}],[{&lt;&lt;"host">>,&lt;&lt;"127.0.0.1:4004">>},{&lt;&lt;"connection">>,&lt;&lt;"keep-alive">>},{&lt;&lt;"content-length">>,&lt;&lt;"0">>},{&lt;&lt;"cache-control">>,&lt;&lt;"no-cache">>},{&lt;&lt;"origin">>,&lt;&lt;"chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop">>},{&lt;&lt;"number">>,&lt;&lt;"233265256262">>},{&lt;&lt;"content-type">>,&lt;&lt;"application/json">>},{&lt;&lt;"user-agent">>,&lt;&lt;"Mozilla/5.0
    (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like
    Gecko) Chrome/32.0.1700.107
    Safari/537.36">>},{&lt;&lt;"postman-token">>,&lt;&lt;"61b7c8f0-b4f8-3bde-8b99-67c141242e5a">>},{&lt;&lt;"accept">>,&lt;&lt;"<em>/</em>">>},{&lt;&lt;"dnt">>,&lt;&lt;"1">>},{&lt;&lt;"accept-encoding">>,&lt;&lt;"gzip,deflate,sdch">>},{&lt;&lt;"accept-language">>,&lt;&lt;"en-US,en;q=0.8">>}],[{&lt;&lt;"content-type">>,{&lt;&lt;"application">>,&lt;&lt;"json">>,[]}},{&lt;&lt;"if-modified-since">>,undefined},{&lt;&lt;"if-none-match">>,undefined},{&lt;&lt;"if-unmodified-since">>,undefined},{&lt;&lt;"if-match">>,undefined},{&lt;&lt;"accept">>,[{{&lt;&lt;"<em>">>,&lt;&lt;"</em>">>,[]},1000,[]}]},{&lt;&lt;"connection">>,[&lt;&lt;"keep-alive">>]}],undefined,[{charset,undefined},{media_type,{&lt;&lt;"text">>,&lt;&lt;"html">>,[]}}],waiting,undefined,&lt;&lt;>>,true,done,[],&lt;&lt;>>,undefined},undefined}},[{cowboy_rest,process_content_type,3,[{file,"src/cowboy_rest.erl"},{line,780}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,529}]}]}</p>
  </blockquote>
  
  <p>What am I doing wrong here? Thanks.</p>