[英]Can simple_bridge in chicago boss be able to get request custom header?
I tried to add custom header "api_key" into the client request but simple bridge seems not picking up the header. 我试图将自定义标头“ api_key”添加到客户端请求中,但是简单的桥接似乎无法拾取标头。
Chicago boss which uses simple bridge has a list of valid headers but then how can I use custom header then? 使用简单网桥的芝加哥老板有一个有效标题的列表,但是那我该如何使用自定义标题呢? Can't I? 不能吗
You can detect a header in a controller or in a filter. 您可以在控制器或过滤器中检测标头。
First method in a controller 控制器中的第一种方法
Read a header from the request in a controller. 从控制器中的请求中读取标头。 Let's say that we need to answer a request based on what the client can accept, then you can do something like this. 假设我们需要根据客户端可以接受的内容回答请求,然后您可以执行类似的操作。
-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.
Second method using filters: 使用过滤器的第二种方法:
create a file under the src/lib following the pattern ProjectName_FilterName_filter.erl 按照模式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}
then you must install the filter in your boss.config inside the boss configs 那么您必须在boss配置中的boss.config中安装过滤器
{controller_filter_modules, [foo_general_filter]}
For more information how to use filters see here 有关如何使用过滤器的更多信息,请参见此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.