0

I would like to send a fairly large JSON file to an API using curl from ubuntu, in order to update the inventory of our machines. Note that the aPI works fine and we already do this for windows machines using invoke-webrequest. Windows command:

$postHash =  ConvertTo-Json(@{
                    "hostName" = $computerName;
                    "macAddress" = $macAddress;
                    "ipAddress" = $ipAddress;
                    "pcModel" = $model;
                    "diskType" = $diskType;
                    "graphicsType" = $graphicsType;
                    "serviceTag" = $serviceTag; 
                    "diskMode" = $diskMode;
                    "wolMode" = $wolMode;
                    "displayType" = $displayType;
                    "displayServiceTag" = $displayServiceTag;
                    "recDate" = $recDate;
                    "sleepState" = $sleepState;
})
Invoke-WebRequest -Method PUT -Body $postHash https://our_api.org/api/inventory/$macAddress/ -ContentType "application/json" -Headers @{"Authorization" = "Token 62d85f430210cd1a827bfdc34cd6c1fb1a64d1"} | Out-Null

Ubuntu command:

user@ubuntu:~$ echo $json
{ "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" }

user@ubuntu:~$ curl -k -L -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" "https://our_api.org/api/inventory/$macAddress/" -d $json
{"hostName":["This field is required."],"pcModel":["This field is required."],"diskType":["This field is required."],"macAddress":["This field is required."],"serviceTag":["This field is required."],"diskMode":["This field is required."],"wolMode":["This field is required."],"sleepState":["This field is required."],"displayType":["This field is required."],"displayServiceTag":["This field is required."],"recDate":["This field is required."],"ipAddress":["This field is required."]}curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: "pcModel"
curl: (6) Could not resolve host: "KAT12",
curl: (6) Could not resolve host: "displayType"
curl: (6) Could not resolve host: "DELL
curl: (6) Could not resolve host: U2311H",
curl: (6) Could not resolve host: "graphicsType"
curl: (6) Could not resolve host: "Microsoft
curl: (6) Could not resolve host: Remote
curl: (6) Could not resolve host: Display
curl: (6) Could not resolve host: Adapter",
curl: (6) Could not resolve host: "displayServiceTag"
curl: (6) Could not resolve host: "HV8XP08Q079L",
curl: (6) Could not resolve host: "ipAddress"
curl: (6) Could not resolve host: "172.16.4.194",
curl: (6) Could not resolve host: "recDate"
curl: (6) Could not resolve host: "2022-10-06
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: "serviceTag"
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: ;

^^

The concern is that I get the following errors,curl: (6) and curl(3), curl 3 is probably due to the fact that we send and receive data with the mac_addresses which have special characters like ": : : : etc.". I've seen that I can put mac address inside double quotes but it doesn't change the error. curl 6 My JSON file looks like this. I have already looked at this post, but I dont know how to modify my json file arrordingly.

[Updated]: If i put my json file in quotes like this:

user@ubuntu:~$ curl --request PUT --header "Accept: application/json" --header "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" "https://our_api.org/api/inventory/$macAddress" -d "$value"
curl: (3) URL using bad/illegal format or missing URL

i get the "curl: (3) URL using bad/illegal format or missing URL" error

I have checked the file format several times and it seems to be the same as the one we send with windows. Does anyone have any idea why I can't post a file with curl this way? Thanks

Andy McRae
  • 525
  • 7
  • 15

1 Answers1

0

I found the reasons why my code was not updating the API

1 - I needed to specify the "Content-Type: "application/json" parameter to tell the API that the data sent hat JSON format otherwise I saw in verbose mode that the data has a "x-www-form-urlencoded" format that the API can't understand.

2 - The data sent with curl must accept a value with the format like this

'{"abc":"def",..,"ghi":"jkl"}'

So I ll have

json='{
  "pcModel": "KAT12",
  "displayType": "DELL U2311H",
  "diskType": "Samsung SSD .."
}'

3 - The curl request must be sent with double quotes around the url and the data "" So the end request looks like this:

    curl -k -L -X "PUT" -H "Content-Type: application/json" \
-H "Accept: application/json" -H "Authorization: Token \
62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" \
--url "https://our_api.org/api/inventory/$macAddress/" \
-d "$json"
Andy McRae
  • 525
  • 7
  • 15