2016-08-06 3 views
1

私は{key, value}のようなタプルを持つphoenix req_headersリストを持っています。タプルの値をリストから取得するにはどうすればよいですか?そのような:どのようにリストから値の1つでタプルを取得できますか?

[{"host", "localhost:4000"}, {"user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"}, {"accept", "application/json, text/plain, */*"}, {"accept-language", "en-US,en;q=0.5"}, {"accept-encoding", "gzip, deflate"}, {"content-type", "application/json;charset=utf-8"}, {"referer", "http://localhost:4000/"}, {"content-length", "16"}, {"connection", "keep-alive"}] 

そして私は「http://localhost:4000」であるキー「ホスト」の値を取得したいです。どうやってやるの?

答えて

6

一般的な解決策は、List.keyfind/3です。プラグインのヘッダーには、特別な機能があります - Plug.Conn.get_req_header/2

get_req_header(conn, "host") 
#=> ["localhost:4000"] 
関連する問題