我尝试将自定义标头“api_key”添加到客户端请求中,但简单的桥接器似乎没有拾取标头。
使用简单桥的芝加哥老板有一个有效标题列表,但是我该如何使用自定义标题呢?我不能吗?
我尝试将自定义标头“api_key”添加到客户端请求中,但简单的桥接器似乎没有拾取标头。
使用简单桥的芝加哥老板有一个有效标题列表,但是我该如何使用自定义标题呢?我不能吗?
您可以在控制器或过滤器中检测标题。
控制器中的第一种方法
从控制器中的请求中读取标头。假设我们需要根据客户端可以接受的内容来回答请求,那么您可以这样做。
-module(foo_customer_controller, [Req]).
-compile(export_all).
read('GET', [Id]) ->
Accept = Req:header("Accept"),
case boss_db:find(Id) of
Result when Accept == "application/json" -> {json, Result};
Result when Accept == "text/html" -> {ok, Result};
end.
使用过滤器的第二种方法:
按照模式 ProjectName_FilterName_filter.erl 在 src/lib 下创建一个文件
-module(foo_general_filter).
-export([before_filter/2]).
before_filter(FilterConfig, RequestContext) ->
Request = proplists:get_value(request, RequestContext),
ApiKey = Request:header("api-key"),
%% Check if ApiKey is valid
{ok, RequestContext}
那么你必须在boss configs中的boss.config中安装过滤器
{controller_filter_modules, [foo_general_filter]}
有关如何使用过滤器的更多信息,请参见此处