如何从http get的正文中获取字符串模式匹配



我正试图用Enum.filterString.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/An  "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/Adatez/i) end)                    
[{"Date", "Sun, 27 Oct 2019 20:47:30 GMT"}]

首先,过滤标头的更好方法如下:

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

正文是一个字符串,因此Enum函数无法使用它。它似乎是某种JSON,因此您可能需要先将其解析为数据结构。你可以使用几个长生不老药库来做到这一点,例如使用毒药:

response.body
|> Poison.decode!()
|> Enum.filter(fn item ->
item["format"] == "docker"
end)
|> IO.inspect()

生产:

[
%{
"attributes" => %{},
"format" => "docker",
"name" => "Taskmaster1",
"type" => "hosted",
"url" => "http://127.0.0.1:8081/repository/Taskmaster1"
}
]

最新更新