Search code examples
sslerlangcowboy

Can't get the data from POST


I'm making a simple secure (SSL) website in Erlang with Cowboy and ErlyDTL to get started. On a page I have a form with two input text fields and a submit button.

<form role="form" method="POST" action="favorite">
  <table>
    <tr>
      <td>
        Color:
      </td>
      <td>
        <input type="text" name="color" required>
      </td>
    </tr>
    <tr>
      <td>
        Number:
      </td>
      <td>
        <input type="number" name="nr" required>
      </td>
    </tr>
    <tr>
      <td>
        <button type="submit" class="btn btn-default">Submitt</button>
      </td>
    </tr>
  </table>
</form>

I get an error in the handler which gets the data of the POST and does something with it. The handler looks like (only handler function):

handle(Req, State) ->
  io:format("handle~n"),
  {ok, BodyQs, _Req} = cowboy_req:body_qs(Req),
  io:format("POST ~p~n", [BodyQs]),

  Color = proplists:get_value(<<"color">>, BodyQs),
  Nr  = proplists:get_value(<<"nr">>, BodyQs),
  io:format("AllValues ~p, ~p~n", [Color, Nr]),

  - rest of code -

The error occurs when I try to get the BodyQs variable. I know this because I get the first io:format text.

The error:

Error in process ... on node ... with exit value: {[reason, {badmatch, {error, timeout}}},{mfa,{favorite_handler, handle,2}},{stacktrace, [{favorite_handler,handle,2,[{file,"src/favorite_handler.erl"},{line,41}]},{cowboy_handler,handler_handle,4,[{file, "src/cowboy_handler...

What is wrong with my code?

Is it possible to get data with POST when using ssl?

EDIT: I put the command for getting the BodyQs in an io:format and I got the data in my terminal but it keeps giving me that error when I try to get my data out of the POST.

io:format("POST data: ~p~n", [cowboy_req:body_qs(Req)]),

Thanks in advance


Solution

  • I found my mistake.

    I gave all my Req variables numbers like (Req, Req2, Req3, ...), at first I ignored them with '_' except when I put some data, like a session, in it.

    So don't ignore the Req variables ;)

    That fixes my problem on to the next one.