我正在尝试使用以下命令将包含mac地址的JSON数据发送到api:
$value={ "pcModel": "KAT12", "displayType": "DELL U2311H", "graphicsType": "Microsoft Remote Display Adapter", "displayServiceTag": "HV8XP08Q079L", "ipAddress": "172.16.4.194", "recDate": "2022-10-06 16:57:55", "serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)", "wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu", "sleepState": "disable", "macAddress": "90:B1:1C:8E:D5:11", "hostName": "CI-KR95-05", "diskMode": "raid", "diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB" }
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18
但我得到了以下答案:
user@ubuntu:~$ curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" -d "$value" --url "https:/my_api/api/inventory/84:2b:2b:a0:0s2:18"
curl: (3) URL using bad/illegal format or missing URL
我试图逃离":"带有\类似的冒号
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84:2b:2b:a0:0s:18"
但我没有得到任何输出,它也没有发送任何信息。
知道如何在不出现格式错误的情况下发送这些数据吗?
非常感谢
我删除了"quot;从$value添加单引号
试试这个:
#!/bin/bash
value='{ "pcModel": "KAT12", "displayType": "DELL U2311H", "graphicsType": "Microsoft Remote Display Adapter", "displayServiceTag": "HV8XP08Q079L", "ipAddress": "172.16.4.194", "recDate": "2022-10-06 16:57:55", "serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)", "wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu", "sleepState": "disable", "macAddress": "90:B1:1C:8E:D5:11", "hostName": "CI-KR95-05", "diskMode": "raid", "diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB" }'
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18"
你妈妈没教你如何格式化吗?
value='{
"pcModel": "KAT12",
"displayType": "DELL U2311H",
"graphicsType": "Microsoft Remote Display Adapter",
"displayServiceTag": "HV8XP08Q079L",
"ipAddress": "172.16.4.194",
"recDate": "2022-10-06 16:57:55",
"serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)",
"wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu",
"sleepState": "disable",
"macAddress": "90:B1:1C:8E:D5:11",
"hostName": "CI-KR95-05",
"diskMode": "raid",
"diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB"
}'
$value
不是bash中用于声明的合法变量名,仅当您想查找它时才使用。使用value=
此外,URL后面缺少一个"
。
$ curl "http:/cake.com/nom-nom
> "
curl: (3) URL using bad/illegal format or missing URL
协议模式后面跟着冒号斜杠://
。所以你还需要一个:
https://
也许你想阅读网址rfc:
https://www.ietf.org/rfc/rfc2718.txt
2.1.2"//"以下":">
与过去几年的一些例子相反,使用双斜杠
作为URL的第一个组件这不仅仅是一个艺术指标,表明下面是一个URL:
只有当URL的语法包含RFC中描述的层次结构时,才使用双斜杠2396.在这种方案的URL中,使用双斜杠表示后面的是命名机构。(有关更多详细信息,请参阅RFC 2396的第3节。(不包含一致层次结构的URL方案结构中不应使用double在":"一串
感谢您的回答。
我发现了我的代码没有更新API 的原因
1-我需要指定CCD_ 5参数来告诉API数据发送的是JSON格式,否则我在详细模式中看到数据具有";x-www-form-urlencoded";API无法理解的格式。
2-用curl发送的数据必须接受类似格式的值
value='{
"pcModel": "KAT12",
"displayType": "DELL U2311H",
"diskType": "Samsung SSD .."
}'
3-curl请求必须在url和数据""
周围用双引号发送所以最终请求看起来是这样的:
curl -k -X "PUT" -H "Content-Type: application/json"
-H "Accept: application/json" -H "Authorization: Token 62d85df902101828g84kc4cd6c1fb1a64d1"
--url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18"
-d "$value"