2015-01-20 56 views

回答

10

jwoddercorrect on the filter但我想经历这一步一步,因为我不熟悉Go数据类型。

Docker API documentation指的是使用map[string][]string用于过滤器,这是一种围棋地图(哈希表)

  • map[string]限定了地图类型string

  • []string的键是类型定义为地图中的值。片 []是一个没有固定长度的数组。然后该切片由 string值组成。

所以API需要包含字符串的数组的哈希映射。这Go Playground演示编组转到过滤数据:

mapS := map[string][]string{ "status":[]string{"exited"} } 

为JSON:

{ "status": [ "exited" ] } 

所以补充说JSON的多克尔API请求你:

GET /containers/json?all=1&filters={%22status%22:[%22exited%22]} 

all=1包括报告退出的容器(如命令行上的-a)。/

+0

我试过在浏览器中使用引号转义并使用curl,但都没有给出结果。如何获得此请求的有效回复? – 2015-01-20 21:49:00

+0

您需要为'exited'添加'all = 1'来报告。回答更新 – Matt 2015-01-20 21:55:51

+0

完美!这就是我所错过的。这对我有效:http:// localhost:5555/containers/json?all = 1&filters = {%22status%22:%5B%22exited%22%5D} – 2015-01-20 22:29:56

2

通过我的文档阅读,它应该是:

GET /containers/json?filters={"status":["exited"]} 

一些可能需要进行网址编码,虽然。

+0

什么是对URL进行编码的正确方法? http:// localhost:5555/containers/json?filters = {%27status%27%3A%5B%27exited%27%5D}不起作用。 – 2015-01-20 21:00:36

+0

curl -X GET http:// localhost:5555/containers/json?filters =%7B%22status%22%3A%5B%22exited%22%5D%7D returned curl -X GET http:// localhost :5555/containers/json?all = 1正确返回 – 2015-01-20 21:21:39

2

使用泊坞窗,卷曲最优雅的方式,并且不与编码我this answer发现麻烦:

如果他们只是记录了JSON结构的API它可能对非围棋的人会更容易。基本上,它告诉curl使用数据作为查询参数并对其进行编码。要获得退出的容器,查询可能如下所示:

curl -G -XGET "http://localhost:5555/containers/json" \ 
    -d 'all=1' \ 
    --data-urlencode 'filters={"status":["exited"]}' | python -m json.tool