I'm trying to get to know ejabberd and erlang...
The first thing i wan't to do is to implement my own set of REST calls to control ejabberd from another server.
for example for this request:
POST /custom/register HTTP/1.1
Host: some.server.com:8088
Cache-Control: no-cache
{"username":"user","host":"test.com","password":"pass1"}
I want to register new Jabber user, so I've created this ejabberd module:
-module(mod_test).
-author('').
-vsn('0.1').
-behavior(gen_mod).
-export([start/2, stop/1,process/2]).
%%
%% Includes
%%
%% base ejabberd headers
-include("ejabberd.hrl").
%% ejabberd compatibility functions
-include("jlib.hrl").
%% ejabberd HTTP headers
-include("web/ejabberd_http.hrl").
start(_Host, _Opts) ->
ok.
stop(_Host) ->
ok.
process(["register"], _Request) ->
Data = _Request#request.data,
{200,[],Data}.
And added the http listener in ejabberd.cfg, I've been able to get the response that contains the post data back, BUT, I cant understand how to extract the the values from the Data.
for example I want that the variable Username will get the value of the "username" key ("user") in the POST data.
Thanks in Advance, Matan.
Ejabberd uses Jiffy which is an Erlang JSON parser. So you can use it to parse your HTTP request POST payload inside the handler.
Request
$ curl \
-XPOST http://localhost:5280/test/register \
--data '{"username":"user","host":"test.com","password":"pass1"}'
Handler
process([<<"register">>], #request{method = 'POST', data = Data}) ->
{JSON} = jiffy:decode(Data),
User = proplists:get_value(<<"username">>, JSON),
Host = proplists:get_value(<<"host">>, JSON),
Pass = proplists:get_value(<<"password">>, JSON),
%% do something with:
%% User =:= <<"user">>
%% Host =:= <<"test.com">>
%% Pass =:= <<"pass1">>
%% ...
{200, [], <<"response">>};
process(_Call, _Req) ->
{404, [], <<"not_found">>}.