Home > Back-end >  Send data to API containing special characters
Send data to API containing special characters

Time:10-10

I am trying to send JSON data containing a mac address to an api using this command:

$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

but I get the following answer:

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

I tried to escape the ":" colon characters with \ like this

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"

but I get no output and it sends nothing.

any Idea how to send this data without having the bad format error?

Thanks a lot

CodePudding user response:

I removed the "?" from $value and added single quotes.

Try this:

#!/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"

CodePudding user response:

Didn't your mother teach you how to format?

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 is not a legal variable name in bash for declaration, only when you want to look it up. use value=

Also, you are missing a " after the URL.

$ curl "http:/cake.com/nom-nom
> "
curl: (3) URL using bad/illegal format or missing URL

The protocol schema is followed by colon-slash-slash ://. so you need one more:

https://

Maybe you want to read the URL rfc:

https://www.ietf.org/rfc/rfc2718.txt

2.1.2 Improper use of "//" following ":"

Contrary to some examples set in past years, the use of double
slashes as the first component of the of a URL is not simply an artistic indicator that what follows is a URL:
Double slashes are used ONLY when the syntax of the URL's contains a hierarchical structure as described in RFC 2396. In URLs from such schemes, the use of double slashes indicates that what follows is the top hierarchical element for a naming authority. (See section 3 of RFC 2396 for more details.) URL schemes which do not contain a conformant hierarchical structure in their should not use double slashes following the ":" string.

  • Related