user10663015
user10663015

Reputation:

curl with Variable not work - Shell Script

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?

Upvotes: 0

Views: 702

Answers (1)

ikegami
ikegami

Reputation: 385789

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.

Upvotes: 1

Related Questions