skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    Thanks for your answers.

    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

    value='{
      "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 -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"
    

  2. 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"
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search