sahilpaudel

sahilpaudel

Destructuring the conn object

my headers

[
  {"accept", "*/*"},
  {"accept-encoding", "gzip, deflate, br"},
  {"authorization",
   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo2LCJzYWx0IjozMzQ4Mjd9.RtWI9w8uuxBue9Byi0tlZm6EUlN83HC1Ed9UzhPHpN4"},
  {"cache-control", "no-cache"},
  {"connection", "keep-alive"},
  {"content-length", "136"},
  {"content-type", "application/json"},
  {"experiment_data",
   "8Qd7CiAgICAiZXhwZXJpbWVudHMiOiBbFQAAAgACHwAEAgD1ByJuYW1lIjogIlZlcnNpb25UZXN0IiwtAAACADAiaWQhABxCFwCLcmVzdWx0IjpSAAACAPoFInhnLWRlbGl2ZXJ5LXgxIjogMjBVAAACAAomAF8yIjogNSYADmkzIjogMzCgABV9DgAWfWQACpQAD+YAATxPbmXSAA/pAJeACiAgICBdCn0="},
  {"host", "localhost:4000"},
  {"postman-token", "af95e65d-1261-4471-8191-b3f29b634254"},
  {"user-agent", "PostmanRuntime/7.26.5"}
]
def evaluate(%{req_headers: [%{"experiment_data"=> experiment_data}]} = conn, _) do
    IO.inspect("===============")
    IO.inspect(experiment_data)
    IO.inspect("===============")

    conn
    |> render_json_response(%{})
  end

I want the evaluate function to be called only when the experiment_data is present in the header and process it else go to other fallback function.
I tried all possible goggling stuff but couldn’t find the solution.

Thanks.

Marked As Solved

und0ck3d

und0ck3d

The req_headers is a list of 2-elem tuples and your matcher on evaluate is trying to match a map inside the list.

My suggestion is that on the function you are calling evaluate you use the Plug.Conn.get_req_header/2 function, that returns a list of values for that header and pattern match against that:

def call(%{req_headers: headers} = conn, _) do
  conn |> Plug.Conn.get_req_header("experiment_data") |> List.first() |> evaluate(conn)
end

def evaluate(nil, conn), do: conn

def evaluate(value, conn) do
  # do something with value
end

Bear in mind it’s possible to pattern match against list’s values:

ExampleA:

def evaluate(%{req_headers: [{"experiment_data", experiment_data} | _]}) do
  ...
end

Example B:

def evaluate(%{req_headers: [{"experiment_data", experiment_data}]}) do
  ...
end

But example A would only work if the {"experiment_data", "value"} tuple was the first element of the list and example B would only work if the list only had one element and that tuple was that element.

Hope I clarified your question :slight_smile:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement