ubuntu 将包含特殊字符的数据发送到API

bpzcxfmw  于 2022-11-02  发布在  其他
关注(0)|答案(3)|浏览(214)

我尝试使用以下命令将包含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"

但我没有得到任何输出,它也不发送任何内容。
知道如何发送这些数据而不出现格式错误吗?
多谢了

nkkqxpd9

nkkqxpd91#

我删除了$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"
d6kp6zgx

d6kp6zgx2#

你妈妈没教过你怎么格式化吗?

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方案不应在“:“字符串后使用双斜杠。

5f0d552i

5f0d552i3#

谢谢你的回答。
我找到了代码不更新API的原因
1 -我需要指定"Content-Type: "application/json"参数来告诉API发送的数据是JSON格式的,否则我在详细模式下看到数据是API无法理解的“x-www-form-urlencoded”格式。
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"

相关问题