【问题标题】:How can I get a tuple from a list by one of the values?如何通过其中一个值从列表中获取元组?
【发布时间】:2016-08-06 16:34:29
【问题描述】:

我有一个 phoenix req_headers 列表,其中包含 {key, value} 之类的元组。如何通过列表中的键获取元组的值?像这样:

[{"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"}]

我想获取键“host”的值,即“http://localhost:4000”。我该怎么做?

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    一般的解决方案是List.keyfind/3。对于 Plug 中的标头,有一个特殊功能 - Plug.Conn.get_req_header/2:

    get_req_header(conn, "host")
    #=> ["localhost:4000"]
    

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2016-03-11
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      相关资源
      最近更新 更多