What version of ejabberd are you using?
18.06
What operating system (version) are you using?
Ubuntu 16.04
How did you install ejabberd (source, package, distribution)?
Linux Installer
What did not work as expected? Are there error messages in the log? What
I have my custom Module which posts some data on through http:request
I wanted to provide URL from from ebbed.yml file like this
mod_custo_post:
url: "http://localhost/webcode/post.php"
but I get this error
{http,{#Ref<0.553383904.99614721.184438>,{error,{failed_connect,[{to_address,{<<"localhost">>,80}},{inet,[inet],{options,{socket_options,[inet,{packet_size,0},{packet,0},{header,0},{active,false},{mode,binary}]}}}]}}}}
But if I add that URL in the module code it works. My module is is reading url mentioned .yml
I think some issue with type conversion.
Url = gen_mod:get_module_opt(Server, ?MODULE, url),
Sep = "&",
Header = [],
ContentType = "application/x-www-form-urlencoded",
Body = [
"user_id=", To, Sep,
"message_from=", From, Sep
],
HTTPOptions = [],
Options = [],
?INFO_MSG("post request ~p~n Binary:~p~n", [Body, list_to_binary(Body)]),
{ok, _ReqId} = httpc:request(post, { "http://localhost/webcode/post.php", Header, ContentType, list_to_binary(Body) }, HTTPOptions, [ {sync, false},{receiver, {?MODULE, post_result, []}} ]),
?INFO_MSG("post request sent Response: ~p~n ", [ _ReqId ]),
was the unexpected behavior? What was the expected result?
Convert it to list inside mod_opt_type/1.
@zinid Thanks . I have fixed the issue by following modification
Previously I was using following code
% mod_opt_type(url) -> fun iolist_to_binary/1;
mod_opt_type(url) ->
fun(Val) when is_binary(Val) -> binary_to_list(Val);
(Val) -> Val
end;
In recent ejabberd versions you can do:
mod_opt_type(url) ->
fun(URL) -> binary_to_list(misc:try_url(URL) end;
The function misc:try_url/1 validates the URL provided and also always returns binary(), so you don't need clauses in your code.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
In recent ejabberd versions you can do:
The function
misc:try_url/1validates the URL provided and also always returnsbinary(), so you don't need clauses in your code.