Can simple_bridge in chicago boss be able to get request custom header?

61 Views Asked by At

I tried to add custom header "api_key" into the client request but simple bridge seems not picking up the header.

Chicago boss which uses simple bridge has a list of valid headers but then how can I use custom header then? Can't I?

1

There are 1 best solutions below

2
On

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

-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

{controller_filter_modules, [foo_general_filter]}

For more information how to use filters see here