如何从此URL获取值?并执行一个带有值的erlang模块来回复带有结果的客户端?

时间:2012-01-04 00:33:49

标签: http erlang yaws erlangweb

http://www.myserver.com/exile?Key1=Value1&Key2=Value2

当我以这种格式向我的服务器发出请求时, 我该如何处理这个请求? 我需要做的是:需要获取所有值并运行Erlang模块,并将结果发送给客户端。它是否放逐CGI,如果是这样,它怎么会没有.cgi扩展名?

其他数据: 我在我的服务器上设置了Yaws(运行Linux服务器的桌面)。 yaws.conf文件已配置。

1 个答案:

答案 0 :(得分:4)

以下是解决方案:

<erl>

out(A)->
    Values = yaws_api:parse_query(A),
    Value1 = proplists:get_value("Key1",Values),
    Value2 = proplists:get_value("Key2",Values),
    %% then do anything with them ....
    %% ....
    {html,"Json Data or HTML tags or XML data or string of data"}.
    %% or {ehtml,[{p,[],""}]}.

<erl>

此处提供更多信息: http://yaws.hyber.org/query.yaws

OR

<erl>

out(A)->
    Value1 = yaws_api:queryvar(A,"Key1"),
    Value2 = yaws_api:queryvar(A,"Key2"),
    %% Need to be careful here
    %% if the value aint found, the 
    %% variable will contain an atom 'undefined'
    %% then do anything with them ....
    %% ....
    {html,"Json Data or HTML tags or XML data or string of data"}.
    %% or {ehtml,[{p,[],""}]}.

<erl>

OR

<erl>

out(A)->
    Value1 = yaws_api:getvar(A,"Key1"),
    Value2 = yaws_api:getvar(A,"Key2"),
    %% Need to be careful here
    %% if the value aint found, the 
    %% variable will contain an atom 'undefined'
    %% then do anything with them ....
    %% ....
    {html,"Json Data or HTML tags or XML data or string of data"}.
    %% or {ehtml,[{p,[],""}]}.

<erl>

详细了解模块: yaws_api.erl

* 注意* 避免使用最后一个选项(getvar/2),因为它首先检查POST数据,然后检查GET数据,查找指定的参数。只有当您不确定参数是否出现在GET或POST请求数据中时,才应该使用它。