【问题标题】:Using Curl and PUT request to edit JSON file使用 Curl 和 PUT 请求编辑 JSON 文件
【发布时间】:2020-04-24 21:00:33
【问题描述】:

我正在尝试编辑 REST API 上的端点,该端点为我提供了一组对象。我想编辑 json 文件,但在格式化 HTTP 请求时遇到了问题。

端点的输出类似于

"result" : [
  {
     "MAC" : "00:08:00:4A:A1:B3",
     "available" : true,
     "bridge" : "br0",
     "ipv4" : {
        "dns1" : "",
        "dns2" : "",
        "gateway" : "",
        "ip" : "",
        "mask" : "",
        "mode" : ""
     },
     "ipv6" : {
        "delegatedPrefixLength" : 64,
        "dns1" : "",
        "dns2" : "",
        "enabled" : false,
        "fixedIp" : [],
        "gateway" : "",
        "ip" : [],
        "linkLocalIp" : [],
        "mode" : "DELEGATED",
        "prefixDelegationEnabled" : false
     },
     "name" : "eth0",
     "nitype" : "ETHER",
     "type" : "LAN"
  },
  {
     "available" : false,
     "bridge" : "br0",
     "ipv4" : {
        "dns1" : "",
        "dns2" : "",
        "gateway" : "",
        "ip" : "",
        "mask" : "",
        "mode" : ""
     },
     "ipv6" : {
        "delegatedPrefixLength" : 64,
        "dns1" : "",
        "dns2" : "",
        "enabled" : false,
        "fixedIp" : [],
        "gateway" : "",
        "ip" : [],
        "linkLocalIp" : [],
        "mode" : "DELEGATED",
        "prefixDelegationEnabled" : false
     },
     "name" : "eth1",
     "nitype" : "ETHER",
     "type" : "LAN"
  }

]

我需要能够在数组的第一个对象中附加一些字段。我试过了

curl -k -X PUT -H "Content-Type: application/json" -d '{[{"ipv4":{"mode":"DHCP"},"name": "eth0", "type":WAN}]"}' https://192.168.2.1/api/ni?token=$token1

但我不断收到错误消息,说它需要一个对象/值/数组。

有什么建议吗?

【问题讨论】:

    标签: json bash curl


    【解决方案1】:

    您的 PUT 数据似乎没有顶级 JSON 字段:

    {[{"ipv4":{"mode":"DHCP"}, ...

    API 可能期待这样的事情:

    {"request": [{"ipv4":{"mode":"DHCP"}, ...

    检查该 API 的文档和示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2018-04-29
      • 2012-11-26
      相关资源
      最近更新 更多