Sunday, August 4, 2013

Erlang Goodness: The Req variable (ChicagoBoss) with POST via ajax

In ChicagoBoss erlang web framework, controllers are what we call "parameterized modules". This allows for something like a dependency injection of variable data - in this case the [Req] variable, which contain a lot of goodies for us.
A standard ChicagoBoss controller would begin with something like this:
-module(myProject_myController_controller, [Req]).

Where the [Req] variable is is a SimpleBridge object. From GitHub: "SimpleBridge takes the pain out of coding to multiple Erlang HTTP servers by creating a standardized interface. It currently supports Mochiweb".

So, when issuing a POST request to our controller, we would expect this kind of thing to work:
PageData = Req:post_param("my_post_data")

However, turns out the Request object expects our POST data in only one format: "forms" format. And by default, and doing HTTP POST with jQuery or Angualr $http, we get the POST params is "request payload" format.

The solution: changing the header in the HTTP POST request. For instance, in Angualr JS, this can be done by passing this to the $http ctor:

headers: {'Content-Type': 'application/x-www-form-urlencoded'}



No comments:

Post a Comment