0

when I execute a normal curl via a shell script functioniert es.

This work:

curl -s -v -X POST --data '{
    "zoneConfig": {
        "userID": "'$userid'",
        "name": "'$myName'",
        "id":"'$id'"
    },
    "delete": [
        {
            "id": "ID1"
        },
        {
            "id": "ID2"
        }
    ]
}' https://urlToAPI

But as soon as I put "delete" in a variable I get an undefined error from the API vendor

This is not working

delete='{
    "id": "ID1"
},
{
    "id": "ID2"
}'

curl -s -v -X POST --data '{
    "zoneConfig": {
        "userID": "'$userid'",
        "name": "'$myName'",
        "id":"'$id'"
    },
    "delete": [
        '$deleteValues'
    ]
}' https://urlToAPI

But I don't understand the difference as both configurations are the same?

7
  • add -trace-ascii dump.txt to the command line and check that file after you've run your command... Commented Sep 5, 2020 at 19:40
  • I get "== Info: Could not resolve host: "200905shwpkzwvw4coy"" Commented Sep 5, 2020 at 19:56
  • ... which hints that you somehow got the argument to --data wrong (quote issues?) so that curl interpreted that sequence as the URL instead of post content. Commented Sep 5, 2020 at 20:33
  • and please drop -X POST it makes my head hurt. Commented Sep 5, 2020 at 20:34
  • @Daniel Stenberg Thank you. But what is beter? When i search on stackoverflow I find many examples woth this Commented Sep 5, 2020 at 21:04

1 Answer 1

1

When interpolating, the value is split on whitespace.[1]

As such,

a='a b c'
prog $a

is equivalent to

prog 'a' 'b' 'c'

This splitting doesn't occur if the interpolation occurs inside of double-quotes.

As such,

a='a b c'
prog "$a"

is equivalent to

prog 'a b c'

Therefore, you need to change

$deleteValues

to

"$deleteValues"

  1. To be precise, the IFS env var controls how the value is split. It's normally set such that splitting occurs on spaces, tabs and line feeds.
Sign up to request clarification or add additional context in comments.

Comments