简体   繁体   English

芝加哥老板中的simple_bridge是否可以获取请求自定义标头?

[英]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.

相关问题 从HTTP请求获取自定义响应头 - Get custom response header from HTTP request 自定义 header 到 HttpClient 请求 - Custom header to HttpClient request 要添加自定义 header 我使用了 HttpServletRequestWrapper 那个 header 我没有进入 @RequestHeader HttpHeader httpHeader 但在请求中 - To add custom header I used HttpServletRequestWrapper that header i dont get in @RequestHeader HttpHeader httpHeader but in request its there 无法在angularjs中设置自定义标头 - Not able to set custom header in angularjs 自定义标头未插入servlet的请求中 - Custom header not inserted in request in servlet 无法在android中的volley中附加带有post请求的标头 - Not able to attach header with post request in volley in android Flutter Web - 无法在 header 中更改 GET 请求的引用者 - Flutter Web - Can't change GET request's referer in header 使用标头发送HTTP GET请求 - Send HTTP GET request with header 如何将自定义 HTTP 标头注入 SuperAgent 发出的每个请求? - How can I inject a custom HTTP Header into every request that SuperAgent makes? 在 java 中自定义 HTTP 请求的情况下,如何设置 Header 映射? - How can i set Header map in case of custom HTTP Request in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM