我正在尝试使用docker远程API使用docker API创建网络,我一直在尝试几乎所有方法,但是我一直收到此错误:json: cannot unmarshal array into Go struct field IPAM.IPAM.Options of type map[string]string。我也用另一种类型收到此错误:type network.IPAMConfig
这里是一些日志,首先是我的配置对象,然后是错误,包括消息,状态和axios配置。

the network config {
  "CheckDuplicate": false,
  "Driver": "bridge",
  "Name": "some-site5_com_net",
  "IPAM": {
    "Driver": "default",
    "Config": [
      {
        "Subnet": "172.0.1.24/29",
        "IPRange": "172.0.1.24/29",
        "Gateway": "172.0.1.25"
      }
    ],
    "Options": []
  }
}
could not create network XHRerror: json: cannot unmarshal array into Go struct field IPAM.IPAM.Options of type map[string]string
    at /home/node/app/server/functions/docker-network.js:47:43
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  status: 400,
  axiosConfig: '{\n' +
    '  "url": "/networks/create",\n' +
    '  "method": "post",\n' +
    '  "data": "{\\"CheckDuplicate\\":false,\\"Driver\\":\\"bridge\\",\\"Name\\":\\"some-site5_com_net\\",\\"IPAM\\":{\\"Driver\\":\\"default\\",\\"Config\\":[{\\"Subnet\\":\\"172.0.1.24/29\\",\\"IPRange\\":\\"172.0.1.24/29\\",\\"Gateway\\":\\"172.0.1.25\\"}],\\"Options\\":[]}}",\n' +
    '  "headers": {\n' +
    '    "Accept": "application/json, text/plain, */*",\n' +
    '    "Content-Type": "application/json;charset=utf-8",\n' +
    '    "User-Agent": "axios/0.20.0",\n' +
    '    "Content-Length": 198\n' +
    '  },\n' +
    '  "transformRequest": [\n' +
    '    null\n' +
    '  ],\n' +
    '  "transformResponse": [\n' +
    '    null\n' +
    '  ],\n' +
    '  "timeout": 0,\n' +
    '  "xsrfCookieName": "XSRF-TOKEN",\n' +
    '  "xsrfHeaderName": "X-XSRF-TOKEN",\n' +
    '  "maxContentLength": -1,\n' +
    '  "maxBodyLength": -1,\n' +
    '  "socketPath": "/var/run/docker.sock"\n' +
    '}'
}
和代码
console.log("the network config", JSON.stringify(networkConfig, null, 2))
const config = {
  method: "POST",
  url: "/networks/create",
  data: networkConfig,
  socketPath: "/var/run/docker.sock"
}

axios(config)
.then(response => { return resolve(response.data.Id) })
.catch(err => {
  if (err.isAxiosError) return reject(new XHRerror(err.response.data.message, err.response.status, err.config))
  return reject(err)
})
一些相关的docker docs
我究竟做错了什么?

最佳答案

您正在为选项字段传递一个空数组

"Options": []
但实际上是带有任意键和字符串值的JSON对象
"Options": {}
(对Go错误消息进行解码,“无法解码数组”表示您向其发送了一个数组值; IPAM.IPAM.Options是该字段的Go名称,但是您可以在请求中看到IPAMOptions;而Go类型map[string]string是 map 或包含字符串键和字符串值的字典,但结构没有更多,对应于无模式JSON对象。)

10-08 04:53