首页 > 解决方案 > 如何从 http get 的正文中获取字符串模式匹配

问题描述

我正在尝试过滤掉我正在使用的这个响应体,Enum.filter并尝试过滤掉这个响应String.match体。我该怎么办?

iex(15)>  response = HTTPoison.get!("http://127.0.0.1:8081/service/rest/v1/repositories")       
%HTTPoison.Response{
  body: "[ {\n  \"name\" : \"maven-snapshots\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-snapshots\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-central\",\n  \"format\" : \"maven2\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-central\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://repo1.maven.org/maven2/\"\n    }\n  }\n}, {\n  \"name\" : \"nuget-group\",\n  \"format\" : \"nuget\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-group\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget.org-proxy\",\n  \"format\" : \"nuget\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget.org-proxy\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://www.nuget.org/api/v2/\"\n    }\n  }\n}, {\n  \"name\" : \"maven-releases\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-releases\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget-hosted\",\n  \"format\" : \"nuget\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-hosted\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-public\",\n  \"format\" : \"maven2\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-public\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"Taskmaster1\",\n  \"format\" : \"docker\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/Taskmaster1\",\n  \"attributes\" : { }\n} ]",
  headers: [
    {"Date", "Sun, 27 Oct 2019 20:39:44 GMT"},
    {"Server", "Nexus/3.19.1-01 (OSS)"},
    {"X-Content-Type-Options", "nosniff"},
    {"Content-Type", "application/json"},
    {"Content-Length", "1411"} 
  ],
  request: %HTTPoison.Request{
    body: "",
    headers: [],
    method: :get,
    options: [],
    params: %{},
    url: "http://127.0.0.1:8081/service/rest/v1/repositories"
  },
  request_url: "http://127.0.0.1:8081/service/rest/v1/repositories",
  status_code: 200
}

这是我收到的错误:

iex(16)> Enum.filter(response.body, fn {key, _} -> String.match?(key, ~r/\A\n  \"format\" : \"docker\"\z/i) end)
** (Protocol.UndefinedError) protocol Enumerable not implemented for "[ {\n  \"name\" : \"maven-snapshots\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-snapshots\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-central\",\n  \"format\" : \"maven2\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-central\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://repo1.maven.org/maven2/\"\n    }\n  }\n}, {\n  \"name\" : \"nuget-group\",\n  \"format\" : \"nuget\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-group\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget.org-proxy\",\n  \"format\" : \"nuget\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget.org-proxy\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://www.nuget.org/api/v2/\"\n    }\n  }\n}, {\n  \"name\" : \"maven-releases\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-releases\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget-hosted\",\n  \"format\" : \"nuget\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-hosted\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-public\",\n  \"format\" : \"maven2\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-public\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"Taskmaster1\",\n  \"format\" : \"docker\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/Taskmaster1\",\n  \"attributes\" : { }\n} ]" of type BitString. This protocol is implemented for the following type(s): Ecto.Adapters.SQL.Stream, DBConnection.Stream, DBConnection.PrepareStream, HashSet, Range, Map, Function, List, Stream, Date.Range, HashDict, GenEvent.Stream, MapSet, File.Stream, IO.Stream
    (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir) lib/enum.ex:141: Enumerable.reduce/3
    (elixir) lib/enum.ex:3023: Enum.filter/2

它适用于标题,但不适用于正文:

iex(16)>  response = HTTPoison.get!("http://127.0.0.1:8081/service/rest/v1/repositories")                       
%HTTPoison.Response{
  body: "[ {\n  \"name\" : \"maven-snapshots\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-snapshots\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-central\",\n  \"format\" : \"maven2\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-central\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://repo1.maven.org/maven2/\"\n    }\n  }\n}, {\n  \"name\" : \"nuget-group\",\n  \"format\" : \"nuget\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-group\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget.org-proxy\",\n  \"format\" : \"nuget\",\n  \"type\" : \"proxy\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget.org-proxy\",\n  \"attributes\" : {\n    \"proxy\" : {\n      \"remoteUrl\" : \"https://www.nuget.org/api/v2/\"\n    }\n  }\n}, {\n  \"name\" : \"maven-releases\",\n  \"format\" : \"maven2\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-releases\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"nuget-hosted\",\n  \"format\" : \"nuget\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/nuget-hosted\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"maven-public\",\n  \"format\" : \"maven2\",\n  \"type\" : \"group\",\n  \"url\" : \"http://127.0.0.1:8081/repository/maven-public\",\n  \"attributes\" : { }\n}, {\n  \"name\" : \"Taskmaster1\",\n  \"format\" : \"docker\",\n  \"type\" : \"hosted\",\n  \"url\" : \"http://127.0.0.1:8081/repository/Taskmaster1\",\n  \"attributes\" : { }\n} ]",
  headers: [
    {"Date", "Sun, 27 Oct 2019 20:47:30 GMT"},
    {"Server", "Nexus/3.19.1-01 (OSS)"},
    {"X-Content-Type-Options", "nosniff"},
    {"Content-Type", "application/json"},
    {"Content-Length", "1411"} 
  ],
  request: %HTTPoison.Request{
    body: "",
    headers: [],
    method: :get,
    options: [],
    params: %{},
    url: "http://127.0.0.1:8081/service/rest/v1/repositories"
  },
  request_url: "http://127.0.0.1:8081/service/rest/v1/repositories",
  status_code: 200
}
iex(17)> Enum.filter(response.headers, fn {key, _} -> String.match?(key, ~r/\Adate\z/i) end)                    
[{"Date", "Sun, 27 Oct 2019 20:47:30 GMT"}]

标签: httpgetelixirphoenix-framework

解决方案


首先,过滤标题的更好方法是这样的:

Enum.filter(response.headers, &match?({"Date", _}, &1))

输出:

[{"Date", "Sun, 27 Oct 2019 20:47:30 GMT"}]

对于正文,您应该首先从 JSON 中对其进行解码。您需要选择一个 JSON 库并将其添加到mix.exs. 我正在使用杰森。然后就可以按照上面的方法进行匹配了:

Jason.decode!(request.body) |> Enum.filter(&match?(%{"name" => "maven-central"}, &1))

输出:

[
  %{
    "attributes" => %{
      "proxy" => %{"remoteUrl" => "https://repo1.maven.org/maven2/"}
    },
    "format" => "maven2",
    "name" => "maven-central",
    "type" => "proxy",
    "url" => "http://127.0.0.1:8081/repository/maven-central"
  }
]

推荐阅读