Skip to main content
added 748 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Your main issue is actually a couple of space characters on these lines in your code:

--data '{
    "commandOverride": ' "$NEXT_JOB_COMMANDS" '
}')

The 2nd single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the middle line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

A variant of the above allows you to avoid writing JSON entirely and instead stores your array as an array in the shell:

NEXT_JOB_COMMANDS=( 'sleep' '200s' )

NEXT_JOB_ID=$(
    formdata=$(
        jq -c -n '{ commandOverride: $ARGS.positional }' --args "${NEXT_JOB_COMMANDS[@]}"
    ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

The above assumes a shell that has named arrays. For the /bin/sh shell, use set -- sleep 200s to set the list of positional parameters to the array elements, and then "$@" to insert that list after --args in the arguments to jq.

Your main issue is actually a couple of space characters on these lines in your code:

--data '{
    "commandOverride": ' "$NEXT_JOB_COMMANDS" '
}')

The 2nd single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the middle line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Your main issue is actually a couple of space characters on these lines in your code:

--data '{
    "commandOverride": ' "$NEXT_JOB_COMMANDS" '
}')

The 2nd single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the middle line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

A variant of the above allows you to avoid writing JSON entirely and instead stores your array as an array in the shell:

NEXT_JOB_COMMANDS=( 'sleep' '200s' )

NEXT_JOB_ID=$(
    formdata=$(
        jq -c -n '{ commandOverride: $ARGS.positional }' --args "${NEXT_JOB_COMMANDS[@]}"
    ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

The above assumes a shell that has named arrays. For the /bin/sh shell, use set -- sleep 200s to set the list of positional parameters to the array elements, and then "$@" to insert that list after --args in the arguments to jq.

added 21 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Your main issue is actually a couple of space characters on this linethese lines in your code:

--data '{
    "commandOverride": ' "$NEXT_JOB_COMMANDS" '
}')

The first2nd single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the first single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the middle line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Your main issue is actually a couple of space characters on this line in your code:

"commandOverride": ' "$NEXT_JOB_COMMANDS" '

The first single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the first single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Your main issue is actually a couple of space characters on these lines in your code:

--data '{
    "commandOverride": ' "$NEXT_JOB_COMMANDS" '
}')

The 2nd single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the middle line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

edited body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Your main issue is actually a couple of space characters on this line in your code:

"commandOverride": ' "$NEXT_JOB_COMMANDS" '

The first single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just ofterafter the first single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Your main issue is actually a couple of space characters on this line in your code:

"commandOverride": ' "$NEXT_JOB_COMMANDS" '

The first single quote breaks out of the single-quoted string which is the option argument to the --data option of curl. The space character just ofter the first single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s 
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Your main issue is actually a couple of space characters on this line in your code:

"commandOverride": ' "$NEXT_JOB_COMMANDS" '

The first single quote breaks out of the single-quoted string, which is the option argument to the --data option of curl. The space character just after the first single quote ensures that the string in $NEXT_JOB_COMMANDS will be its own argument to curl, and you then add } (preceded by a newline) as an additional extra argument, probably confusing curl even more.

You could solve this by changing the line to

"commandOverride": '"$NEXT_JOB_COMMANDS"'

... but you could also use jq to insert your $NEXT_JOB_COMMANDS value into the form body (which would ensure that the resulting JSON body is well formed and valid).


If you want to pass a well-formed JSON document (your array) as a top-level key in a JSON document used with --data in your curl invocation, you may do as follows:

NEXT_JOB_COMMANDS='["sleep", "200s"]'

NEXT_JOB_ID=$(
    formdata=$( jq -c -n --argjson commandOverride "$NEXT_JOB_COMMANDS" '$ARGS.named' ) &&
    curl -s \
        --header 'Content-Type: application/json' \
        --data "$formdata" \
        'http://localhost:5050/processes/gdalEcho/execution'
)

printf '%s\n' "$NEXT_JOB_ID"

Here, the shell variable formdata (which is local to the command substitution) will get the value

{"commandOverride":["sleep","200s"]}

This is created using jq, which will insert the given JSON fragment as the value for a commandOverride key.

Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading